asp.net – 使用表单验证模拟
|
我有一个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。谷歌怎么做,如果你不知道如何。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET MVC中是否有嵌套主页?
- asp.net – 如果在Application_Start中抛出未处理的异常会发
- ASP.NET MVC 3 jQuery验证;禁用不引人注意的OnKeyUp?
- asp.net – IIS Web服务器中的此错误(扩展配置)是什么?
- asp.net – 如何确定用户的浏览器是否可以查看PDF文件
- asp.net-mvc – 静态文件请求正在ASP.NET MVC3中托管代码
- asp.net-mvc – MVC发布IPagedList
- asp.net – 如何传递datetime参数?
- asp.net-mvc – 如何在ASP.net MVC 4 RouteConfig.cs中使用
- asp.net-mvc – 为IIS托管的.SVC文件配置XML-RPC行为?
- asp.net – 生成PDF,IE和HTTPS错误
- asp.net-mvc-4 – 我似乎没有安装SignalR与MVC4
- asp.net – 文件下载问题:文件名与空格被截断!
- C#,.Net自动生成大写字母编码
- asp.net – Request.Url.AbsoluteUri和重写的URL
- asp.net – 用于验证的数据注释,至少一个必填字段
- asp.net – 向GridView Row添加ID
- asp.net-mvc – ASP.Net MVC – 处理不好的URL参
- asp.net – 如何在MVC4的部分视图中添加脚本?
- asp.net-mvc – 如何使用EF Code First解释为枚举
