繁体   English   中英

强制Windows身份验证并允许匿名

[英]Force windows authentication and allow anonymous

我的想法是让家人使用他们的Windows用户名输入应用程序(只是使他们不必编写它),并使用身份将密码和其他内容保存到本地MDF数据库中。 问题是我无法弄清楚如何强制执行Windows身份验证(以便Context.User.Identity.Name为我提供Windows用户名)并使用该信息登录到Identity数据库。 为了创建项目,我将Web窗体模板与“个人帐户”作为安全性类型一起使用,并删除了所有Owin第三方登录程序包(Microsoft,Google等)。
这是我尝试过的:

Default.aspx.cs (我的需要身份验证的主页)

protected void Page_Load(object sender, EventArgs e)
{
    //According to Identity comments, ApplicationCookie is the AuthenticationType...
    //once logged in through Identity
    if (Context.User.Identity.AuthenticationType != "ApplicationCookie")
        Response.Redirect("Login.aspx", true);
}

Login.aspx.cs

protected void LogIn(object sender, EventArgs e) //login button handler
{
    var manager = Context.GetOwinContext().GetUserManager<UserAdministrator>();
    var signinManager = Context.GetOwinContext().GetUserManager<SessionAdministrator>();

    string windowsName = Context.User.Identity.Name;
    User user = manager.Users.Where(u => u.UserName == windowsName).FirstOrDefault();
    // rest of the login code...
}

web.config (全局)

<location path="Login.aspx"> //this should only allow windows logged-in users
  <system.web>
      <authorization>
        <allow users="*" />
        <deny users="?" />
     </authorization>
  </system.web>
</location>
<location path="default.aspx"> // this should only allow Identity-logged in users
  <system.web>
    <authorization>
      <allow users="*" />
      <deny users="?" />
    </authorization>
  </system.web>
</location>

项目性质

  • Windows身份验证设置为启用
  • 匿名身份验证设置为禁用

由于某种原因,启动应用程序并浏览到default.aspx或login.aspx时,不会使用Windows身份验证,因此Context.User.Identity.IsAuthenticated返回false。 我怎样才能实现自己想要的?

尝试在default.aspx节点中删除<allow users="*" />

它将通过拒绝匿名来强制应用使用身份验证

可以认为这是可以解决的。

我删除了Windows身份验证并切换到窗体身份验证,并使用发现的以下代码获取Windows用户名(不记得是谁回答了SO问题):

System.Security.Principal.WindowsPrincipal windowsUser = new System.Security.Principal.WindowsPrincipal(Request.LogonUserIdentity);
Request.LogonUserIdentity.Impersonate();
string username = windowsUser.Identity.Name.Substring(windowsUser.Identity.Name.LastIndexOf("\\") + 1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM