Crypto API problem while using forms authentication

M

Michael Ulmann

Hi,

I'm developing a webapplication. I would like to use forms authentication
instead of integrated windows authentication because i don't like this pop
window to log on. My users are stored in the active directory and in order
to get access to the database (sql server) i need to impersonate.

Unfortunately i got the following error in the line
("FormsAuthentication.SetAuthCookie( Context.User.Identity.Name, false ))")
after impersonate:

my source:

public class index : MasterPage {

public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);

[DllImport("advapi32.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]
public extern static int DuplicateToken(IntPtr hToken,

int impersonationLevel,
ref IntPtr hNewToken);
protected TextBox txtUserName;
protected TextBox txtPassword;
protected System.Web.UI.WebControls.Label output;
protected HyperLink lnkLogin;

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
lnkLogin.Attributes.Add ("onClick", "fnLogin(); return false;");
lnkLogin.NavigateUrl = "#";
lnkLogin.Text = "Login";
}
else
{
if (CheckLogin (txtUserName.Text, txtPassword.Text))
{
FormsAuthentication.SetAuthCookie
(Context.User.Identity.Name,false);
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text,
false);
}
}
}

private bool CheckLogin (string user, string pass)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if(LogonUser(user, ConfigurationSettings.AppSettings["DomainName"],
pass, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null) return true;
else return false;
}
else return false;
}
else return false;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top