using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FormsAuth;
public partial class Logon : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//UnJ]Login^
protected void Login_Click(object sender, EventArgs e)
{
string adPath = @"LDAP://DC=Cathaybk, DC=intra, DC=uwccb"; //Path to your LDAP directory server
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
if(true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
{
string groups = adAuth.GetGroups();
//Create the ticket, and add the groups.
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);
//Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
//Create a cookie, and then add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
if(true == isCookiePersistent)
authCookie.Expires = authTicket.Expiration;
//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);
//You can redirect now.
// Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
Response.Redirect("./Default.aspx");
}
else
{
errorLabel.Text = "{Ҩä\CˬdϥΪ̦W٩αKXC]Authentication did not succeed. Check user name and password.^";
}
}
catch(Exception ex)
{
errorLabel.Text = "{ҥ " + ex.Message;
}
}
}