Crypto API problem while using forms authentication

From: Michael Ulmann (ukmichael_at_hotmail.ch)
Date: 09/01/04


Date: Wed, 1 Sep 2004 14:54:32 +0200

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;
}

}



Relevant Pages

  • Re: LogonUser()
    ... // This sample can be run only on Windows XP. ... String lpszPassword, ... int dwLogonType, int dwLogonProvider, ref IntPtr phToken); ... private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr ...
    (microsoft.public.dotnet.security)
  • Re: Validating a user using SSPI
    ... > My problem is trying to authenticate using windows ... Here's some code that you can use: [warning: untested air code] ... static extern int LogonUser(string Username, string Domain, string Password, ...
    (microsoft.public.dotnet.security)
  • Crypto API problem while using forms authentication
    ... instead of integrated windows authentication because i don't like this pop ... public static extern int LogonUser(String lpszUserName, ... String lpszPassword, ... protected TextBox txtUserName; ...
    (microsoft.public.dotnet.security)
  • Re: C# messaging
    ... The string you specify here needs to be a unique string in the Windows ... will get an int returned to you so save it. ... use the same exact string as you did in your C app). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: why use the sealed ?
    ... system that supports inheritance and polymorphism is to promote reusability. ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... >> exceeds past the end of the string. ... >> application to be a Remoting Client. ...
    (microsoft.public.dotnet.languages.csharp)