ASP.NET : VMware의 도메인에 대해 가장합니다
-
07-07-2019 - |
문제
VMware 시스템에서 실행되는 ASP.NET 애플리케이션에서 도메인 사용자로서 자신을 사칭해야합니다. VMware 시스템 자체가 도메인에 있지 않기 때문에 ASP.NET은 사용자 토큰 (Web.Config에 지정 됨)을 해결할 수 없습니다. 그렇게 할 방법이 있습니까?
미리 감사드립니다, petr
해결책
나는 항상 쓴이 수업을 사용하며 매력처럼 작동합니다!
using System;
using System.Security.Principal;
/// <summary>
/// Changes the security context the application runs under.
/// </summary>
public class ImpersonateHelper : IDisposable
{
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
private IntPtr _token = IntPtr.Zero;
private WindowsImpersonationContext _impersonatedUser = null;
public IntPtr Token
{
get { return _token; }
set { _token = value; }
}
public ImpersonateHelper(IntPtr token)
{
_token = token;
}
/// <summary>
/// Switch the user to that set by the Token property
/// </summary>
public void Impersonate()
{
if (_token == IntPtr.Zero)
_token = WindowsIdentity.GetCurrent().Token;
_impersonatedUser = WindowsIdentity.Impersonate(_token);
}
/// <summary>
/// Revert to the identity (user) before Impersonate() was called
/// </summary>
public void Undo()
{
if (_impersonatedUser != null)
_impersonatedUser.Undo();
}
#region IDisposable Members
private bool _isDisposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
{
if (_impersonatedUser != null)
_impersonatedUser.Dispose();
}
CloseHandle(_token);
_token = IntPtr.Zero;
}
_isDisposed = true;
}
~ImpersonateHelper()
{
Dispose(false);
}
#endregion
}
그런 다음 클라이언트 클래스에서 다음과 같이 부릅니다.
//Run task as the impersonated user and not as NETWORKSERVICE or ASPNET (in IIS5)
try{
impersonate.Impersonate();
//Do work that needs to run as domain user here...
}
finally
{
//Revert impersonation to NETWORKSERVICE or ASPNET
if (impersonate != null)
{
impersonate.Undo();
impersonate.Dispose();
}
}
행운을 빕니다!
다른 팁
이것은 멍청한 대답 일지 모르지만 VMware 시스템을 도메인에 추가 할 수 있습니다.
제휴하지 않습니다 StackOverflow