ASP.NET 4.5 Web表单混合身份验证(集成模式)

时间:2015-12-09 14:18:59

标签: c# asp.net forms-authentication windows-authentication mixed-authentication

我正在尝试为我正在开发的内部网站实现以下方案:

  • 当用户登录与该网站位于同一域的PC并连接到与该网站相同的网络时,访问该Intranet站点时,他将自动登录(Windows身份验证)
  • 当具有相同PC的同一用户登录到与网站相同的域但在其他网络(例如家庭网络)上时,作为网站,他将自动登录(Windows身份验证)< / LI>
  • 当同一个用户从他/她的家庭PC(不属于域和网络)打开网站时,他将被重定向到表单身份验证登录页面并能够使用他/她的AD凭据登录(使用AD Membership进行表单身份验证)
  • 如果其他用户没有AD帐户且不在域名的PC上,他将被重定向到表单身份验证登录页面,并且可以使用他的身份登录/她的SQL用户scredentials(使用SQL成员身份进行表单身份验证)

除了一个重要的步骤,我几乎有这个工作。我现在的代码就是:

Global.asax中

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        // we only want 302 redirects if they are for login purposes
        if (this.Response.StatusCode == 302 && this.Response.RedirectLocation.Contains("/WinLogin"))
        {
            if (Request.UserHostAddress.StartsWith("10.1.34") || Request.UserHostAddress.StartsWith("10.1.35"))
            {
                this.Response.StatusCode = 401;
                // note that the following line is .NET 4.5 or later only
                // otherwise you have to suppress the return URL etc manually!
                this.Response.SuppressFormsAuthenticationRedirect = true;
            }
        }
    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated && HttpContext.Current.User.Identity is WindowsIdentity)
        {
            // note that we will be stripping the domain from the username as forms authentication doesn't capture this anyway

            // create a temp cookie for this request only (not set in response)
            var tempCookie = FormsAuthentication.GetAuthCookie(Regex.Replace(HttpContext.Current.User.Identity.Name, ".*\\\\(.*)", "$1", RegexOptions.None), false);

            // set the user based on this temporary cookie - just for this request
            // we grab the roles from the identity we are replacing so that none are lost
            HttpContext.Current.User = new GenericPrincipal(new FormsIdentity(FormsAuthentication.Decrypt(tempCookie.Value)), (HttpContext.Current.User.Identity as WindowsIdentity).Groups.Select(group => group.Value).ToArray());

            // now set the forms cookie
            FormsAuthentication.SetAuthCookie(HttpContext.Current.User.Identity.Name, false);
        }
    }

WinLogin.aspx:

var membershipProvider1 = Membership.Providers["MyAdMembershipProvider"];
var membershipProvider2 = Membership.Providers["MySqlMembershipProvider"];
if (membershipProvider1.ValidateUser(TextBox1.Text, TextBox2.Text) || membershipProvider2.ValidateUser(TextBox1.Text, TextBox2.Text))
{
    // set the forms auth cookie
    FormsAuthentication.SetAuthCookie(TextBox1.Text, false);

    // reset request.isauthenticated
    var authCookie = System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
    if (authCookie != null)
    {
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        if (authTicket != null && !authTicket.Expired)
        {
            var roles = authTicket.UserData.Split(',');
            System.Web.HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(authTicket), roles);
        }
    }

    FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
}

的Web.config

<authentication mode="Forms">
  <forms name=".ASPXFORMSDEMO" loginUrl="WinLogin.aspx" />
</authentication>
<authorization>
  <deny users="?"  />
  <allow users="*"   />
</authorization>

<location path="/WinLogin.Aspx">
  <system.web>
    <authorization>
      <allow users="?"/>
    </authorization>
  </system.web>
</location>

IIS中的设置

网站根级

  • 启用匿名身份验证
  • 表单身份验证已启用
  • Windows身份验证已启用

WebLogin.aspx级别

  • 启用匿名身份验证
  • 表单身份验证已启用
  • Windows身份验证已禁用

我的所有场景都工作,除了我定义的部分,我是否可以使用Windows身份验证。通过我现在的设置,只有我本地网络中的PC才能继续使用Windows身份验证。每当我在网络外部使用我的电脑时,我都会获得表格认证登录屏幕。

有没有人建议我如何确定是否应该使用Windows身份验证。我一直在使用当前用户登录的用户名并检查它的域并使用它,但我似乎无法从网站的IIS设置获得

提前致谢!

0 个答案:

没有答案