LOGON API and persistent cookies (Windows and Form authentication)

B

Ben Ong

Hi i integrated windows and forms authentication on my apps. I created
a logon page and used the windows authentication instead of managing
the password and username myself. I can authenticate just fine, but
when i go to set a persistent cookie it(the app) doesnt seem to let me.
I have to always go through the login aspx file. All my code compiles
and runs correctly accept for the part when the persistent cookie is
suppose to be working. here's a snippet of my code:

// this is just sitting in my Login class. not under any method.
// it also allows me to authenticate through windows
[DllImport("C:\\WINDOWS\\System32\\advapi32.dll")]
public static extern bool
LogonUser(String lpszUsername,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
out int phToken);

// this validates the forms username and password

public bool validateUser(string uid,string pwd)
{
try
{
int token1;
bool loggedOn =
LogonUser(uid,"NOI",pwd,2,0,out token1);
IntPtr token2 = new IntPtr(token1);
WindowsIdentity wi =
new WindowsIdentity(token2);
WindowsPrincipal wp =
new WindowsPrincipal(wi);
HttpContext.Current.User = wp;
return true;
}
catch(Exception)
{
return false;
}
}
// this supposedly creates the persistent cookie

private void btnLogin_Click(object sender, System.EventArgs e)
{
/*
* Trace debugging
*/
Trace.Warn("LoginPage.btnLogin_Click(): Testing!");
Trace.Warn( "Logger: " + LOG.Logger.Name );
Trace.Warn( "Log debug: " + LOG.Logger.IsEnabledFor( Level.DEBUG ));
LOG.Info("Login.btnLogin_Click: Testing!");

if (validateUser(txtUsername.Text,txtPassword.Text))
{
// FormsAuthentication.RedirectFromLoginPage( txtUsername.Text,
true );

//
FormsAuthentication.SetAuthCookie(Context.User.Identity.Name,true);
// Response.Redirect("Default.aspx");
FormsAuthentication.SetAuthCookie(txtUsername.Text, true);
FormsAuthentication.RedirectFromLoginPage( txtUsername.Text, true
);
}
else
{
msgLogin.Text = "Invalid Login.";
msgLogin.ForeColor = Color.Red;
msgLogin.ToolTip = "Username and password do not match.";
}
}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top