Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ASP.NET成员资格提供程序进行模拟_Asp.net_.net_Asp.net Membership_Impersonation - Fatal编程技术网

使用ASP.NET成员资格提供程序进行模拟

使用ASP.NET成员资格提供程序进行模拟,asp.net,.net,asp.net-membership,impersonation,Asp.net,.net,Asp.net Membership,Impersonation,我有一个自定义的成员/角色提供程序,由于项目的性质,它需要管理员以用户身份登录,同时帮助他们进行查询 现在,它很容易重新登录管理员与所选的会员帐户,但这意味着管理员将有效地注销。我正在寻找一种方法,允许管理员模拟用户,但在任何时候都可以很容易地切换回自己的帐户 有什么建议吗?这应该是你想要的 您可以使用所需域帐户的用户名和密码调用ImpersonateValidUser方法。然后在注销时将其反转 您应该能够将其弯曲以与您的自定义成员资格提供商一起使用 // Constants for imper

我有一个自定义的成员/角色提供程序,由于项目的性质,它需要管理员以用户身份登录,同时帮助他们进行查询

现在,它很容易重新登录管理员与所选的会员帐户,但这意味着管理员将有效地注销。我正在寻找一种方法,允许管理员模拟用户,但在任何时候都可以很容易地切换回自己的帐户


有什么建议吗?

这应该是你想要的

您可以使用所需域帐户的用户名和密码调用ImpersonateValidUser方法。然后在注销时将其反转

您应该能够将其弯曲以与您的自定义成员资格提供商一起使用

// Constants for impersonation
private WindowsImpersonationContext impersonationContext;
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

/// <summary>
/// Changes the account we are running under. 
/// </summary>
/// <param name="username">Username of a local admin account</param>
/// <param name="domain">Domain of the username</param>
/// <param name="password">Password of a local admin account</param>
/// <returns></returns>
private 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;
}

/// <summary>
/// Cancel the impersonation and revent the thread to the
/// default account. Typically DOMAIN\NETWORK_SERVICE or similar.
/// </summary>
private void UndoImpersonation()
{
    impersonationContext.Undo();
}
//模拟的常量
私有WindowsImpersonationContext impersonationContext;
public const int LOGON32\u LOGON\u INTERACTIVE=2;
public const int LOGON32_PROVIDER_DEFAULT=0;
/// 
///更改我们正在使用的帐户。
/// 
///本地管理员帐户的用户名
///用户名的域
///本地管理员帐户的密码
/// 
private bool ImpersonateValidUser(字符串用户名、字符串域、字符串密码)
{
WindowsIdentity tempWindowsIdentity;
IntPtr令牌=IntPtr.Zero;
IntPtr-tokenDuplicate=IntPtr.Zero;
if(restortoself())
{
if(LogonUserA(用户名、域、密码、LOGON32\u登录\u交互、,
LOGON32\u提供者\u默认值,ref令牌)!=0)
{
if(DuplicateToken(token,2,ref tokenDuplicate)!=0)
{
tempWindowsIdentity=新的WindowsIdentity(令牌复制);
impersonationContext=tempWindowsIdentity.Impersonate();
if(impersonationContext!=null)
{
闭合手柄(令牌);
闭合手柄(重复);
返回true;
}
}
}
}
if(令牌!=IntPtr.Zero)
闭合手柄(令牌);
如果(令牌重复!=IntPtr.Zero)
闭合手柄(重复);
返回false;
}
/// 
///取消模拟并将线程转发给
///默认帐户。通常为域\网络\服务或类似服务。
/// 
私有无效撤消模拟()
{
impersonationContext.Undo();
}

看起来这与广告账户有很大关系-:我认为这对我没有帮助。