Wednesday, March 4, 2015

how to send referrer header value with javascript

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 = ''
f.method = 'get' = '_parent'

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 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)
               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

Your PC IP is , port 3000

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

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

now try to connect again using your mobile 

Friday, December 12, 2014


if you get this error in ajax / updatepanel, one probable cause is 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)

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[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")]
    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") ||
throw new Exception("Invalid file type");

SafeTokenHandle safeTokenHandle;
//This parameter causes LogonUser to create a primary token.

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

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

using (StreamWriter sw = new StreamWriter(File.Open(path, FileMode.Create), Encoding.UTF8))