/********************************************/
/**** SOURCE CODE EXAMPLE ****/
/********************************************/
using System;
using System.Security.Principal;
using System.Security.Permissions;
using System.Runtime.InteropServices;
namespace Sanjeev.NETArchitect.Apps.Security.AuthenticateAndAuthorize
{
public class cSiteImpersonation
{
#region "Declarations"
private enum Logon
{
Network = 3,
NetworkCleartext = 8
}
private enum Provider
{
UseDefault = 0,
WindowsNT35 = 1,
WindowsNT40 = 2,
Windows2000 = 3
}
#endregion
#region "Public Functions"
public WindowsImpersonationContext ImpersonateUser(string sUserName, string sPassword, string sDomain)
{
//-----------------------------------------
// Function: ImpersonateUser
// Description: Changes to windows identity given the input parameters.
//
// Inputs: sUserName - The User to impersonate
// sPassword - The Password for the impersonated User.
// sDomain - The Domain that is being accessed
//
// Outputs: WindowsImpersonationContext, the Context for the Impersonation
//-----------------------------------------
WindowsIdentity objNewIdentity = default(WindowsIdentity);
objNewIdentity = GetWindowsIdentity(sUserName, sDomain, sPassword);
return objNewIdentity.Impersonate;
}
public WindowsIdentity GetCurrentIdentity()
{
//-----------------------------------------
// Function: GetCurrentIdentity
// Description: Returns the current windows identity.
//
// Inputs: None
//
// Outputs: WindowsIdentity, the current identity
//-----------------------------------------
return WindowsIdentity.GetCurrent();
}
public WindowsIdentity RevertIdentity(WindowsImpersonationContext CurrentImpersonation)
{
//-----------------------------------------
// Function: RevertIdentity
// Description: Undoes the impersonation, which reverts to the windows identity
// prior to the impersonation
//
// Inputs: None
//
// Outputs: WindowsIdentity, the current identity
//-----------------------------------------
CurrentImpersonation.Undo();
}
#endregion
#region "Private Functions"
[DllImport("advapi32.dll")]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref int phToken);
//-----------------------------------------
// Function: LogonUser
// Description: Uses the windows32 advapi32.dll to logon to the windows
// environment as another user.
//
// Inputs: lpszUsername - The User to impersonate
// lpszDomain - The Domain that is being accessed
// lpszPassword - The Password for the impersonated User
// dwLogonType - Logon type
// dwLogonProvider - The type of windows provider
// phToken - Returned Security Token
//
// Outputs: Boolean, Success or Failure
//-----------------------------------------
[DllImport("Kernel32.dll")]
private static extern int GetLastError();
//-----------------------------------------
// Function: GetLastError
// Description: Used the Kernal32.dll to get the last error .
//
// Inputs: None
//
// Outputs: WindowsIdentity, the Context for the Impersonation
//-----------------------------------------
[SecurityPermissionAttribute(SecurityAction.Demand, ControlPrincipal = true, UnmanagedCode = true)]
private static WindowsIdentity GetWindowsIdentity(string UserName, string Domain, string Password)
{
//-----------------------------------------
// Function: GetWindowsIdentity
// Description: Logon the user and return the windows identity given
// the input parameters.
//
// Inputs: sUserName - The User to impersonate
// sDomain - The Domain that is being accessed
// sPassword - The Password for the impersonated User.
//
// Outputs: WindowsIdentity, the Context for the Impersonation
//-----------------------------------------
int SecurityToken = 0;
bool Success = false;
Success = LogonUser(UserName, Domain, Password, Logon.Network, Provider.UseDefault, SecurityToken);
if (!Success)
{
throw new Exception("Logon Failed. Error: " + GetLastError());
}
return new WindowsIdentity(new IntPtr(SecurityToken));
}
#endregion
}
}
/********************************************/
No comments:
Post a Comment