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 System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security;
using System.Security.Permissions;
public partial class Client_Novelis_Login : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Image image = new Image();
tdTopMiddle.Style.Add("background-image", image.ResolveClientUrl("~/Client/Novelis/Images/table_side_top.gif"));
tdSideLeft.Style.Add("background-image", image.ResolveClientUrl("~/Client/Novelis/Images/table_side_left.gif"));
tdSideRight.Style.Add("background-image", image.ResolveClientUrl("~/Client/Novelis/Images/table_side_right.gif"));
tdBottomMiddle.Style.Add("background-image", image.ResolveClientUrl("~/Client/Novelis/Images/table_side_bottom.gif"));
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string ADskipaccount = "admin";
string ADDomain = "mydomain";
string ASPNETDefPasswd = "mypass999";
Impersonator oImper = null;
e.Authenticated = false; //not authenticated.
try
{
Login oLogin = (Login)sender;
if (oLogin.UserName.ToLower().StartsWith(ADskipaccount))
//if valid skip account, auth thru ASPNET
e.Authenticated = Membership.ValidateUser(oLogin.UserName, oLogin.Password);
else
{
oImper = new Impersonator(oLogin.UserName, ADDomain, oLogin.Password);
oImper.Impersonate();
MembershipUserCollection oUserCol = Membership.FindUsersByName(oLogin.UserName);
MembershipUser oUser;
if (oUserCol.Count == 0) //if not found, create user with default password
{
oUser = Membership.CreateUser(oLogin.UserName, ASPNETDefPasswd);
if (oUser != null)
{
//Log "create user success"
}
else
{
//Log "create user failed"
}
}
else
{
oUser = oUserCol[oLogin.UserName];
}
if (Membership.ValidateUser(oLogin.UserName, ASPNETDefPasswd))
{
e.Authenticated = oUser.IsApproved;
}
}
}
catch(Exception ex)
{
//Log "AD Authentication failed"
e.Authenticated = false;
oImper.Undo();
}
}
protected void Login1_LoggedIn(object sender, EventArgs e)
{
string desiredUrl = Request.QueryString[QS.RETURN_URL];
if (desiredUrl != null)
{
Response.Redirect("~" + desiredUrl.Replace(Request.ApplicationPath, ""));
}
}
protected void UserName_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox tb = (TextBox)sender;
tb.Focus();
}
}
}
public class Impersonator
{
// private members for holding domain user account credentials
private string username = String.Empty;
private string password = String.Empty;
private string domain = String.Empty;
// this will hold the security context for reverting back to the client after impersonation operations are complete
private WindowsImpersonationContext impersonationContext = null;
// disable instantiation via default constructor
private Impersonator()
{ }
public Impersonator(string username, string domain, string password)
{
// set the properties used for domain user account
this.username = username;
this.domain = domain;
this.password = password;
}
private WindowsIdentity Logon()
{
IntPtr handle = new IntPtr(0);
handle = IntPtr.Zero;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_PROVIDER_DEFAULT = 0;
// attempt to authenticate domain user account
bool logonSucceeded = LogonUser(this.username, this.domain, this.password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, ref handle);
if (!logonSucceeded)
{
// if the logon failed, get the error code and throw an exception
int errorCode = Marshal.GetLastWin32Error();
throw new Exception("User logon failed. Error Number: " + errorCode);
}
// if logon succeeds, create a WindowsIdentity instance
WindowsIdentity winIdentity = new WindowsIdentity(handle);
// close the open handle to the authenticated account
CloseHandle(handle);
return winIdentity;
}
public void Impersonate()
{
// authenticates the domain user account and begins impersonating it
this.impersonationContext = this.Logon().Impersonate();
}
public void Undo()
{
if (this.impersonationContext != null)
// rever back to original security context which was store in the WindowsImpersonationContext instance
this.impersonationContext.Undo();
}
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool CloseHandle(IntPtr handle);
}