加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 使用表单验证模拟

发布时间:2020-12-16 00:43:36 所属栏目:asp.Net 来源:网络整理
导读:我有一个ASP.NET网站必须使用窗体身份验证而不是Windows身份验证访问ActiveDirectoryMembershipProvider。网站必须使用表单,因为它们需要设计的输入表单,而不是Windows身份验证使用的浏览器身份验证弹出窗口。 该网站需要模拟通过Active Directory登录的用
我有一个ASP.NET网站必须使用窗体身份验证而不是Windows身份验证访问ActiveDirectoryMembershipProvider。网站必须使用表单,因为它们需要设计的输入表单,而不是Windows身份验证使用的浏览器身份验证弹出窗口。

该网站需要模拟通过Active Directory登录的用户访问用户特定的文件。

然而,WindowsIdentity.GetCurrent()与HttpContext.Current.User.Identity不同,尽管我的web.config包含:

<authentication mode="Forms">
    <forms loginUrl="login.aspx" timeout="480"/>
</authentication>
<identity impersonate="true" />

我不能使用LoginUser()和WindowsIdentity.Impersonate(),因为我需要模拟作为AD用户获取他们的特定权限,而且我不知道用户的密码,因为Forms负责登录。

是否可能通过login.aspx.cs来获取System.Web.UI.WebControls.Login.Password,然后将LoginUser()标记保存在WindowsIdentity.Impersonate()的会话变量中?或者也许是一个更安全的假冒正确方法的方法?

我很困惑为什么Forms身份验证不能自动< identity impersonate =“true”/>

我已经阅读了这个http://msdn.microsoft.com/en-us/library/ms998351.aspx,但它使用Windows身份验证。

解决方法

可以使用Forms Authentication来模拟用户。以下代码可以正常工作。

罗伯特提到的Visual Studio Magazine article是一个很好的资源。文章中的示例代码有一些问题,所以我在下面包括一些工作代码。

注意:如果您正在使用Visual Studio,请确保启动“以管理员身份运行”,以避免UAC阻止模拟的问题。

// in your login page (hook up to OnAuthenticate event)
protected void LoginControl_Authenticate(object sender,AuthenticateEventArgs e)
{
    int token;
    // replace "YOURDOMAIN" with your actual domain name
    e.Authenticated = LogonUser(LoginUser.UserName,"YOURDOMAIN",LoginUser.Password,8,out token);
    if (e.Authenticated) {
        Session.Add("principal",new WindowsPrincipal(new WindowsIdentity(new IntPtr(token))));
    }
}

[DllImport("advapi32.dll",SetLastError = true)]
public static extern bool LogonUser(string lpszUsername,string lpszDomain,string lpszPassword,int dwLogonType,int dwLogonProvider,out int TokenHandle);


// in global.asax.cs
void Application_PreRequestHandlerExecute(object send,EventArgs e)
{
    if (Thread.CurrentPrincipal.Identity.IsAuthenticated == true && HttpContext.Current.Session != null) {
        WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Session["principal"];
        Session["principal"] = (GenericPrincipal)Thread.CurrentPrincipal;
        Thread.CurrentPrincipal = windowsPrincipal;
        HttpContext.Current.User = windowsPrincipal;
        HttpContext.Current.Items["identity"] = ((WindowsIdentity)windowsPrincipal.Identity).Impersonate();
    }
}

// in global.asax.cs
void Application_PostRequestHandlerExecute(object send,EventArgs e)
{
    if (HttpContext.Current.Session != null && Session["principal"] as GenericPrincipal != null) {
        GenericPrincipal genericPrincipal = (GenericPrincipal)Session["principal"];
        Session["principal"] = (WindowsPrincipal)Thread.CurrentPrincipal;
        Thread.CurrentPrincipal = genericPrincipal;
        HttpContext.Current.User = genericPrincipal;
        ((WindowsImpersonationContext)HttpContext.Current.Items["identity"]).Undo();
    }
}

// test that impersonation is working (add this and an Asp:Label to a test page)
protected void Page_Load(object sender,EventArgs e)
{
    try {
        // replace YOURSERVER and YOURDB with your actual server and database names
        string connstring = "data source=YOURSERVER;initial catalog=YOURDB;integrated security=True";
        using (SqlConnection conn = new SqlConnection(connstring)) {
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT SUSER_NAME()",conn);
            using (SqlDataReader rdr = cmd.ExecuteReader()) {
                rdr.Read();
                Label1.Text = "SUSER_NAME() = " + rdr.GetString(0);
            }
        }
    }
    catch {
    }
}

更新:

你也应该处理Application_EndRequest,因为像Response.End()这样的调用将绕过Application_PostRequestHandlerExecute。

另一个问题是WindowsIdentity可能会收集垃圾,因此您应该在每次请求时从登录标记创建一个新的WindowsIdentity和WindowsPrincipal。

UPDATE2:

我不知道为什么这会被下调,因为它有效。我添加了pinvoke签名和一些测试代码。再次,使用“以管理员身份运行”启动Visual Studio。谷歌怎么做,如果你不知道如何。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读