ASP.NET:模拟 VMWare 上的域

发布于 2024-07-08 20:00:12 字数 147 浏览 6 评论 0原文

我需要在 VMWare 计算机上运行的 ASP.NET 应用程序中将自己模拟为域用户。 由于 VMWare 计算机本身不在域中,因此 ASP.NET 无法解析用户令牌(在 web.config 中指定)。 有没有办法做到这一点?

提前致谢, 彼得

I need to impersonate myself as a domain user in a ASP.NET application running on VMWare machine. Since the VMWare machine is not itself in the domain, ASP.NET is unable to resolve the user token (specified in web.config). Is there a way to do that?

Thanks in advance,
Petr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2024-07-15 20:00:13

我一直使用我写的这个类,它就像一个魅力!

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();
            }
}

祝您好运!

I use this class I wrote all the time and it works like a charm!

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
}

Then you call it from the client class as:

//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();
            }
}

Good Luck!

心奴独伤 2024-07-15 20:00:13

这可能是一个显而易见的愚蠢答案,但您可以将您的 VMWare 计算机添加到域中。

This may be the dumb obvious answer, but you could add your VMWare machine to the domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文