Wednesday, March 4, 2015

how to send referrer header value with javascript window.open()

referrer http header is only sent if a user click or a server sends a redirect command. so what can we do only using client side code? just simulate a button click!



Sunday, February 22, 2015

redirecting iframe's parent window - cross domain - javascript

just use form with the target as _parent, and the parent window will redirect, even if the domain is different, because parent.location.href etc... will result in browser security error


var f = document.createElement('form')
f.action = 'http://www.thenewurl.com/'
f.method = 'get'
f.target = '_parent'
document.body.appendChild(f)
f.submit()


Friday, January 23, 2015

System.Drawing - How to DrawString in high quality format?


g is a System.Drawing.Graphics object

g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit


Wednesday, January 7, 2015

how to change the html content of asp.net controls rendering/output

let's say you have a textbox ABC. and for that textbox you want the append some strings in it


ABC.SetRenderMethodDelegate(Sub(h As HtmlTextWriter, c As Control)

h.Write("Hello Textbox!")

end sub)


Some controls won't work, such as because they don't implement Render function. Solution is to wrap these kinds of control inside a Placeholder and set the SetRenderMethodDelegate to the Placeholder instead

Example : Let's say you want to append the string " Good Bye" in the output of xml web control

Code Front

< asp:placeholder id="p" runat="server" >

 < asp:xml runat="server" id="x" transformsource="sheet.xslt" />

< / asp:placeholder >






Code Behind

x.DocumentSource = "Some Xml"

p.SetRenderMethodDelegate(Sub(h As HtmlTextWriter, c As Control)

Dim sb As New Text.StringBuilder
Dim tw As New System.IO.StringWriter(sb)
Dim htmlTW As New HtmlTextWriter(tw)
x.RenderControl(htmlTW)
               h.write(sb.ToString() & " - Good Bye ")

end sub)

Monday, January 5, 2015

PhoneGap Desktop / Developer App - Timeout

If you get Timeout in the Phonegap Developer App when trying to connect to your development computer's IP address, one probable cause is the desktop app did not bind to the IP properly. And you have already opened that port in firewall settings

Example
Your PC IP is 192.168.1.10 , port 3000

One solution in windows is to direct that IP and port to localhost (127.0.0.1)


type netsh in command prompt (run as administrator)
>interface
>portproxy
>add v4tov4 listenaddress=192.168.1.10 listenport=3000 connectaddress=127.0.0.1 connectport=3000


now try to connect again using your mobile 

Friday, December 12, 2014

Sys.WebForms.Res.PRM_ServerError

if you get this error in asp.net ajax / updatepanel, one probable cause is asp.net detects "Potentially dangerous form data submit". just make sure you encode text input value before submitting, or set ValidateRequest="false"

Friday, November 14, 2014

how to impersonate a certain windows user in your .net application (or dll)

(also applies to sql clr assembly)

below sample is a sql clr assembly that writes a text to file. compile the project (release) with unsafe checked (project properties > sql clr > permission level)




using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Text;
using System.Security.Permissions;
using System.Security.Principal;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Runtime.ConstrainedExecution;
using System.Security;

public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeTokenHandle()
: base(true)
{
}

[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);

protected override bool ReleaseHandle()
{
return CloseHandle(handle);
}
}

public partial class StoredProcedures
{

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);


[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);

[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void texttofile (string text, string path)
    {

if (path.ToLower().EndsWith("aspx") ||
path.ToLower().EndsWith("ashx") ||
path.ToLower().EndsWith("config") ||
path.ToLower().EndsWith("dll") ||
path.ToLower().EndsWith("exe"))
throw new Exception("Invalid file type");

using(WindowsIdentity.GetCurrent().Impersonate())
{
SafeTokenHandle safeTokenHandle;
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;


bool returnValue = LogonUser("MyWindowsUsername", "MyDomain", "MyPassword",
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeTokenHandle);


using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(safeTokenHandle.DangerousGetHandle()))
{

using (StreamWriter sw = new StreamWriter(File.Open(path, FileMode.Create), Encoding.UTF8))
{
sw.WriteLine(text);
}
}

}


    }
}