使用 asp.net 表单身份验证进行用户模拟
我编写了一个小型 ASP.NET 3.5 应用程序,允许用户自行更新选定的帐户属性。
当我使用基本身份验证时,一切工作正常,但由于显示的对话框不太理想,我想使用表单身份验证来为用户提供有关如何登录的更多说明。
我的问题是,为了用户要更新他们的帐户信息,我必须让应用程序模拟他们进行更新操作。
我在互联网上搜索,试图找到解决我的问题的方法,但没有任何合适或有效的方法。我尝试设置web.config:
<identity impersonate="true">
但这似乎不起作用。 我也有使用 WindowsImpersonationContext 类的 C# 代码,但仍然没有运气。
protected void titleTextBox_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
string fieldTitle = "job title";
string fieldName = "title";
if (userDirectoryEntry == null)
CaptureUserIdentity();
try
{
WindowsImpersonationContext impersonationContext = userWindowsIdentity.Impersonate();
if (String.IsNullOrEmpty(tb.Text))
userDirectoryEntry.Properties[fieldName].Clear();
else
userDirectoryEntry.InvokeSet(fieldName, tb.Text);
userDirectoryEntry.CommitChanges();
impersonationContext.Undo();
PostBackMessages.Add(fieldTitle, "");
}
catch (Exception E)
{
PostBackMessages.Add(fieldTitle, E.Message);
}
}
我还尝试使用 LogonUser 方法创建用户令牌并以这种方式后端身份验证,但它也不起作用。
IntPtr token = IntPtr.Zero;
bool result = LogonUser(userName, domainName, passwordTB.Text, LogonSessionType.Network, LogonProvider.Default, out token);
if (result)
{
WindowsPrincipal wp = new WindowsPrincipal(new WindowsIdentity(token));
System.Threading.Thread.CurrentPrincipal = wp;
HttpContext.Current.User = wp;
if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(usernameTB.Text, false);
}
else
{
FormsAuthentication.SetAuthCookie(usernameTB.Text, false);
}
}
我只是忍不住认为我错过了一些非常简单的事情......
I've written a small ASP.NET 3.5 application to allow users to update selected account attributes on their own.
Everything works fine when I use Basic Authentication, but because the dialog that is presented is less than ideal, I'd like to use forms authentication to give the users more instruction on how to log in.
My problem is that in order for the user to update their account information, I have to have the application impersonate them for the update actions.
I've scoured the internet trying to find a solution to my issue, but nothing fits or works. I have tried setting theweb.config:
<identity impersonate="true">
but that doesn't seem to work.
I also have the C# code using theWindowsImpersonationContext class, but still no luck.
protected void titleTextBox_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
string fieldTitle = "job title";
string fieldName = "title";
if (userDirectoryEntry == null)
CaptureUserIdentity();
try
{
WindowsImpersonationContext impersonationContext = userWindowsIdentity.Impersonate();
if (String.IsNullOrEmpty(tb.Text))
userDirectoryEntry.Properties[fieldName].Clear();
else
userDirectoryEntry.InvokeSet(fieldName, tb.Text);
userDirectoryEntry.CommitChanges();
impersonationContext.Undo();
PostBackMessages.Add(fieldTitle, "");
}
catch (Exception E)
{
PostBackMessages.Add(fieldTitle, E.Message);
}
}
I also tried using theLogonUser method to create a user token and backend the authentication that way, and it doesn't work either.
IntPtr token = IntPtr.Zero;
bool result = LogonUser(userName, domainName, passwordTB.Text, LogonSessionType.Network, LogonProvider.Default, out token);
if (result)
{
WindowsPrincipal wp = new WindowsPrincipal(new WindowsIdentity(token));
System.Threading.Thread.CurrentPrincipal = wp;
HttpContext.Current.User = wp;
if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(usernameTB.Text, false);
}
else
{
FormsAuthentication.SetAuthCookie(usernameTB.Text, false);
}
}
I just can't help but think that I'm missing something incredibly simple...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否启用了 Windows 身份验证并禁用了匿名身份验证在IIS 中?
如果在 ASP.NET 应用程序中启用了模拟,则:
• 如果在 IIS 中启用了匿名访问,则使用 IUSR_machinename 帐户发出请求。
• 如果在IIS 中禁用匿名访问,则将使用经过身份验证的用户的帐户发出请求。
Have you enabled Windows Authentication and disabled Anonymous Authentication in IIS?
If impersonation is enabled in an ASP.NET application then:
• If anonymous access is enabled in IIS, the request is made using the IUSR_machinename account.
• If anonymous access is disabled in IIS, the request is made using the account of the authenticated user.