3

我已经编写了一个小型ASP.NET 3.5应用程序,允许用户自行更新所选帐户属性。当我使用基本身份验证,但因为所出现的对话框中不太理想,我想使用窗体身份验证给用户如何登录 更多的指令使用asp.net表单身份验证的用户模拟

,一切工作正常。

我的问题是,为了让用户更新他们的帐户信息,我必须让应用程序模拟他们进行更新操作。

我已经搜索了互联网试图找到解决我的问题,但没有适合或工作。我已经尝试设置 web.config中

<identity impersonate="true"> 

,但似乎并没有工作。 我也有C#代码使用 WindowsImpersonationContext类,但仍然没有运气。

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

我不禁想,我失去了一些东西非常简单...

回答

1

你有enabled Windows Authentication和禁用匿名身份验证在IIS中?

如果在ASP.NET应用程序中启用了模拟,则:
•如果在IIS中启用了匿名访问,则使用IUSR_machinename帐户进行请求。
•如果在IIS中禁用匿名访问,则使用经过身份验证的用户的帐户进行请求。

+1

为了使表单身份验证正常工作,我必须启用匿名身份验证。尝试通过IIS直接模仿时,由于委派问题,我无法使用Windows身份验证,而且基本身份验证(虽然起作用)笨重且不便于用户使用。 – user437050 2010-09-07 19:38:37