我正在尝试为我正在开发的内部网站实现以下方案:
除了一个重要的步骤,我几乎有这个工作。我现在的代码就是:
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中的设置
网站根级
WebLogin.aspx级别
我的所有场景都工作,除了我定义的部分,我是否可以使用Windows身份验证。通过我现在的设置,只有我本地网络中的PC才能继续使用Windows身份验证。每当我在网络外部使用我的电脑时,我都会获得表格认证登录屏幕。
有没有人建议我如何确定是否应该使用Windows身份验证。我一直在使用当前用户登录的用户名并检查它的域并使用它,但我似乎无法从网站的IIS设置获得
提前致谢!