Class WindowsSecurityContext
Impersonate a Windows Account
public class WindowsSecurityContext : SecurityContext, IOptionHandler
- Inheritance
-
WindowsSecurityContext
- Implements
- Inherited Members
Remarks
This SecurityContext impersonates a Windows account.
How the impersonation is done depends on the value of Impersonate(object). This allows the context to either impersonate a set of user credentials specified using username, domain name and password or to revert to the process credentials.
Constructors
WindowsSecurityContext()
Default constructor
public WindowsSecurityContext()
Remarks
Default constructor
Properties
Credentials
Gets or sets the impersonation mode for this security context
public WindowsSecurityContext.ImpersonationMode Credentials { get; set; }
Property Value
- WindowsSecurityContext.ImpersonationMode
The impersonation mode for this security context
Remarks
Impersonate either a user with user credentials or revert this thread to the credentials of the process. The value is one of the WindowsSecurityContext.ImpersonationMode enum.
The default value is User
When the mode is set to User the user's credentials are established using the UserName, DomainName and Password values.
When the mode is set to Process no other properties need to be set. If the calling thread is impersonating then it will be reverted back to the process credentials.
DomainName
Gets or sets the Windows domain name for this security context
public string DomainName { get; set; }
Property Value
- string
The Windows domain name for this security context
Remarks
The default value for DomainName is the local machine name taken from the MachineName property.
This property must be set if Credentials is set to User (the default setting).
Password
Sets the password for the Windows account specified by the UserName and DomainName properties.
public string Password { set; }
Property Value
- string
The password for the Windows account specified by the UserName and DomainName properties.
Remarks
This property must be set if Credentials is set to User (the default setting).
UserName
Gets or sets the Windows username for this security context
public string UserName { get; set; }
Property Value
- string
The Windows username for this security context
Remarks
This property must be set if Credentials is set to User (the default setting).
Methods
ActivateOptions()
Initialize the SecurityContext based on the options set.
public void ActivateOptions()
Remarks
This is part of the IOptionHandler delayed object activation scheme. The ActivateOptions() method must be called on this object after the configuration properties have been set. Until ActivateOptions() is called this object is in an undefined state and must not be used.
If any of the configuration properties are modified then ActivateOptions() must be called again.
The security context will try to Logon the specified user account and capture a primary token for impersonation.
Exceptions
- ArgumentNullException
The required UserName, DomainName or Password properties were not specified.
Impersonate(object)
Impersonate the Windows account specified by the UserName and DomainName properties.
public override IDisposable Impersonate(object state)
Parameters
stateobjectcaller provided state
Returns
- IDisposable
An IDisposable instance that will revoke the impersonation of this SecurityContext
Remarks
Depending on the Credentials property either impersonate a user using credentials supplied or revert to the process credentials.