本文整理汇总了C#中System.Security.Principal.WindowsImpersonationContext类的典型用法代码示例。如果您正苦于以下问题:C# WindowsImpersonationContext类的具体用法?C# WindowsImpersonationContext怎么用?C# WindowsImpersonationContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WindowsImpersonationContext类属于System.Security.Principal命名空间,在下文中一共展示了WindowsImpersonationContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RevertToImpersonatedUser
public static void RevertToImpersonatedUser(WindowsImpersonationContext _serviceAccountContext)
{
if (_serviceAccountContext != null)
{
_serviceAccountContext.Undo();
}
}
示例2: ImpersonationData
public ImpersonationData(IntPtr TokenHandle, IntPtr DupeTokenHandle, WindowsIdentity Identity, WindowsImpersonationContext ImpersonatedUser)
{
this.TokenHandle = TokenHandle;
this.DupeTokenHandle = DupeTokenHandle;
this.Identity = Identity;
this.ImpersonatedUser = ImpersonatedUser;
}
示例3: Impersonate
public void Impersonate()
{
Authenticated = false;
// Remove the current impersonation by calling RevertToSelf()
if (RevertToSelf())
{
Authenticated = LogonUserA(_username, _domain, _password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
out _handle) != 0;
if (!Authenticated)
{
ErrorCode = Marshal.GetLastWin32Error();
}
// Make a copy of the token for the windows identity private member.
if (DuplicateToken(this._handle, 2, out this._handleDuplicate) != 0)
{
// set the private member for the current impersonation context.
this._context = WindowsIdentity.Impersonate(this._handleDuplicate.DangerousGetHandle());
}
}
}
示例4: Impersonate
public void Impersonate(string domainName, string userName, string password)
{
try
{
const int logon32ProviderDefault = 0;
const int logon32LogonInteractive = 2;
TokenHandle = IntPtr.Zero;
var returnValue = LogonUser(
userName,
domainName,
password,
logon32LogonInteractive,
logon32ProviderDefault,
ref TokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine("LogonUser call failed with error code : " + ret);
throw new System.ComponentModel.Win32Exception(ret);
}
NewId = new WindowsIdentity(TokenHandle);
ImpersonatedUser = NewId.Impersonate();
}
catch (Exception ex)
{
Console.WriteLine("Exception occurred. " + ex.Message);
}
}
示例5: Impersonate
public void Impersonate(string domainName, string userName, string password){
//try
{
// Use the unmanaged LogonUser function to get the user token for
// the specified user, domain, and password.
const int LOGON32_PROVIDER_DEFAULT = 0;
// Passing this parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
tokenHandle = IntPtr.Zero;
// ---- Step - 1
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(
userName,
domainName,
password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle); // tokenHandle - new security token
if (false == returnValue){
int ret = Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(ret);
}
// ---- Step - 2
WindowsIdentity newId = new WindowsIdentity(tokenHandle);
// ---- Step - 3
{
impersonatedUser = newId.Impersonate();
}
}
}
示例6: PerformanceCounterRetriever
public PerformanceCounterRetriever(string server, string domain, string user, string password)
{
if (string.IsNullOrWhiteSpace(server))
throw new ArgumentException($"Null/blank {nameof(server)} specified");
if (string.IsNullOrWhiteSpace(domain))
throw new ArgumentException($"Null/blank {nameof(domain)} specified");
if (string.IsNullOrWhiteSpace(user))
throw new ArgumentException($"Null/blank {nameof(user)} specified");
if (password == null)
throw new ArgumentNullException(nameof(password));
try
{
var userHandle = new IntPtr(0);
var logonSuccess = LogonUser(user, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, ref userHandle);
if (!logonSuccess)
throw new Exception("LogonUser failed");
_identity = new WindowsIdentity(userHandle);
_context = _identity.Impersonate();
_server = server;
_disposed = false;
}
finally
{
Dispose();
}
}
示例7: Enter
public void Enter()
{
if (this.IsInContext)
{
return;
}
this.token = new IntPtr(0);
try
{
this.token = IntPtr.Zero;
bool logonSuccessfull = NativeMethods.LogonUser(
this.username,
this.domain,
this.password,
Logon32LogonInteractive,
Logon32ProviderDefault,
ref this.token);
if (logonSuccessfull == false)
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);
}
var identity = new WindowsIdentity(this.token);
this.context = identity.Impersonate();
}
catch
{
// Catch exceptions here
}
}
示例8: Enter
public void Enter()
{
if (this.IsInContext) return;
m_Token = new IntPtr(0);
try
{
m_Token = IntPtr.Zero;
bool logonSuccessfull = LogonUser(
m_Username,
m_Domain,
m_Password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref m_Token);
if (logonSuccessfull == false)
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);
}
WindowsIdentity identity = new WindowsIdentity(m_Token);
m_Context = identity.Impersonate();
}
catch (Exception exception)
{
// Catch exceptions here
}
}
示例9: Impersonate
public void Impersonate()
{
//create identity
WindowsIdentity newId = new WindowsIdentity(this.tokenHandle);
//start impersonating
this.impersonatedUser = newId.Impersonate();
}
示例10: WindowsImpersonatedIdentity
/// <summary>
/// Constructor. Starts the impersonation with the given credentials. Please note that the account that instantiates the Impersonator class needs to have the 'Act as part of operating system' privilege set.
/// </summary>
/// <param name="userName"> The name of the user to act as. </param>
/// <param name="domainName"> The domain name of the user to act as. </param>
/// <param name="password"> The password of the user to act as. </param>
public WindowsImpersonatedIdentity(string userName, string domainName, string password)
{
var token = IntPtr.Zero;
var tokenDuplicate = IntPtr.Zero;
try {
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(domainName) && string.IsNullOrEmpty(password)) {
identity = WindowsIdentity.GetCurrent();
} else {
if (LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) {
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) {
identity = new WindowsIdentity(tokenDuplicate);
impersonationContext = identity.Impersonate();
} else {
throw new Win32Exception(Marshal.GetLastWin32Error());
}
} else {
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
} finally {
if (token != IntPtr.Zero) {
CloseHandle(token);
}
if (tokenDuplicate != IntPtr.Zero) {
CloseHandle(tokenDuplicate);
}
}
}
示例11: impersonateValidUser
//public void Page_Load(Object s, EventArgs e)
//{
// if (impersonateValidUser("username", "domain", "password"))
// {
// //Insert your code that runs under the security context of a specific user here.
// undoImpersonation();
// }
// else
// {
// //Your impersonation failed. Therefore, include a fail-safe mechanism here.
// }
//}
/// <summary>
/// 模擬使用者
/// </summary>
/// <param name="userName">使用者名稱</param>
/// <param name="domain">網域名稱</param>
/// <param name="password">密碼</param>
/// <returns>True:模擬成功;False:模擬失敗</returns>
public static bool impersonateValidUser(String userName,
String domain,
String password
)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
示例12: Impersonate
/// <summary>
/// 사용자를 가장합니다.
/// </summary>
/// <param name="userName">사용자 이름 입니다.</param>
/// <param name="domain">도메인 이름 입니다.</param>
/// <param name="password">암호 입니다.</param>
/// /// <exception cref="SecurityException">가장이 실패할 경우 <c>SecurityException</c>를 던집니다.</exception>
public void Impersonate(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return;
}
}
}
}
if (token != IntPtr.Zero) CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate);
throw new SecurityException("사용자를 가장하는데 실패했습니다.");
}
示例13: Impersonation
private Impersonation(string domain, string username, string password, LogonType logonType)
{
IntPtr handle;
var ok = NativeMethods.LogonUser(username, domain, password, (int)logonType, 0, out handle);
if (!ok)
{
var errorCode = Marshal.GetLastWin32Error();
throw new ApplicationException(string.Format("Could not impersonate the elevated user. LogonUser returned error code {0}.", errorCode));
}
var profileInfo = new ProfileInfo();
profileInfo.dwSize = Marshal.SizeOf(profileInfo);
profileInfo.lpUserName = username;
profileInfo.dwFlags = 1;
ok = NativeMethods.LoadUserProfile(handle, ref profileInfo);
if (ok == false)
{
var errorCode = Marshal.GetLastWin32Error();
throw new ApplicationException(string.Format("Could not load profile. Error code {0}.", errorCode));
}
NativeMethods.UnloadUserProfile(handle, profileInfo.hProfile);
_handle = new SafeTokenHandle(handle);
_context = WindowsIdentity.Impersonate(_handle.DangerousGetHandle());
}
示例14: Impersonate
public bool Impersonate(string userName, string domain, string password)
{
var token = IntPtr.Zero;
var tokenDuplicate = IntPtr.Zero;
if(RevertToSelf())
{
if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out token))
{
if(DuplicateToken(token, 2, out tokenDuplicate) != 0)
{
var tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
_impersonationContext = tempWindowsIdentity.Impersonate();
if(_impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token != IntPtr.Zero)
{
CloseHandle(token);
}
if(tokenDuplicate != IntPtr.Zero)
{
CloseHandle(tokenDuplicate);
}
return false;
}
示例15: Impersonate
public void Impersonate()
{
// Create the new Identity from the token
WindowsIdentity impersonatedID = new WindowsIdentity(this._tokenHandle);
// Start impersonating as the new ID
this._impersonatedUser = impersonatedID.Impersonate();
}