Cominciamo con la configurazione ruoli server (questo è sotto il responsabile del server, i ruoli, IIS)
Avrai vuole fare in modo che le finestre di autenticazione e le sezioni auth anonimi sono abilitati/installati, e anche le forme auth (che presumibilmente hai già). Dopo quelli sono installati/configurato, è necessario definire quanto segue roba:
Nel web.config si sta andando a voler avere le seguenti sezioni definite:
<configuration>
<system.web>
<authentication mode="Forms">
<forms cookieless="UseDeviceProfile" defaultUrl="~/Default.aspx" enableCrossAppRedirects="true" loginUrl="~/WindowsLogin.aspx" name=".ASPXAUTH" path="/" protection="All" requireSSL="false" slidingExpiration="true" timeout="10080"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
<location path="Login.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true"/>
<windowsAuthentication enabled="false"/>
</authentication>
</security>
</system.webServer>
</location>
<location path="WindowsLogin.aspx">
<system.web>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
</system.web>
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="false"/>
<windowsAuthentication enabled="true"/>
</authentication>
</security>
</system.webServer>
</location>
</configuration>
Allora ti ha bisogno di due file:
Login.aspx (this does forms auth)
WindowsLogin.aspx (this does Windows auth)
LOGIN fa forme, a destra, in modo che è solo Bog Standard ASP.NET forma di autenticazione e 'WindowsLogin che fa la magia (e qui è il file)
using System;
using System.Web;
using System.Web.Security;
using App_Code.Biz;
public partial class WindowsLogin : System.Web.UI.Page {
protected string UserIsInRoles = string.Empty;
private static readonly BAL _mBAL = new BAL();
protected void Page_Load(object sender, EventArgs e) {
string redirectUrl = Request["returnurl"] ?? "~/default.aspx";
string username = Request.ServerVariables["LOGON_USER"];
try {
if (Roles.GetRolesForUser(username).Length < 1)
Roles.AddUserToRole(username, Global.defaultRole);
int status;
_mBAL.aspnet_Membership_CreateUser(username, out status);
} catch (Exception ex) {
ErrHandler.WriteXML(ex);
}
/* Test to see if the user is in any roles */
if (Roles.GetRolesForUser(username).Length < 1) {
UserIsInRoles = "<br />" + username + "You are not in any rules. This must be your first visit to our site!<br /> Adding you to the " + Global.defaultRole + " role now!";
} else {
UserIsInRoles = "You are in the following roles: ";
string[] roles = Roles.GetRolesForUser(username);
foreach (string role in roles)
UserIsInRoles += role + ", ";
UserIsInRoles = UserIsInRoles.Remove(UserIsInRoles.Length - 2) + "!";
if (Login(username, String.Join(",", roles)))
Response.Redirect(redirectUrl);
}
//we shouldn't get here, so if we do, redirect back to a page they can use.
if (Page.IsPostBack) {
if (Response.StatusCode == 401)
Response.Redirect("~/Login.aspx");
}
}
private bool Login(string strUser, string strRole) {
if (strRole != null) {
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // version
strUser, // user name
DateTime.Now, // create time
DateTime.Now.AddYears(1), // expire time
false, // persistent
strRole); // user data
string strEncryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, strEncryptedTicket);
Context.Response.Cookies.Add(cookie);
return true;
}
return false;
}
}
Dopo tutto ciò, si potrebbe ottenere un errore di configurazione per la sezione bloccata a livello di genitore. Il blocco è o per impostazione predefinita (overrideModeDefault = "Nega") o impostato esplicitamente da un tag di posizione ... e in tal caso, il modo più veloce per risolvere è aprire C: \ Windows \ System32 \ inetsrv \ config \ applicationHost.config e modificare il seguente blocco:
<configSections>
<sectionGroup name="system.webServer">
<sectionGroup name="security">
<sectionGroup name="authentication">
<section name="anonymousAuthentication" overrideModeDefault="Allow">
<section name="windowsAuthentication" overrideModeDefault="Allow">
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
vedere anche il registro di chat: http://chat.stackoverflow.com/rooms/5/conversation/configuring-iis7-and-mixed-mode-authentication-in-asp-net
Questo gioca esattamente come hai descritto, compreso l'errore di blocco alla fine molto. Sono preoccupato per le implicazioni della tua "correzione" suggerita. Sto aprendo alcune vulnerabilità o potenziali problemi sovrascrivendo il comportamento predefinito in questo modo? Sembra un po 'come un hack. Questo è così vecchio ... mi chiedo se c'è un modo migliore ora? – ctb
Microsoft lo nega per impostazione predefinita come misura di sicurezza. Tutto ciò che viene modificato per impostazione predefinita nella metabase di IIS sembra un trucco. Probabilmente c'è una qualche funzione dell'interfaccia utente che ti consente di cambiarla, ma non riesco a scoprire dove si trova. – jcolebrand
Dipende da quale si desidera venire prima – jcolebrand