J'ai résolu ce problème. De la manière suivante:
web.config
:
<authentication mode="Forms">
<forms name="APPAUTH"
defaultUrl="/webapp/Home.mvc"
loginUrl="/webapp/Session.mvc/Login"
protection="All"
timeout="30"
path="/"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<location path="Session">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
Je crochet Application_AuthenticateRequest
quelque chose le long des lignes de:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (null == authCookie)
{
//no authentication cokie present
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch (Exception)
{
// Can't do anything if we can't decrypt the ticket so treat it as not there
FormsAuthentication.SignOut(); // Remove bad ticket
}
if (authTicket == null)
{
//could not decrypt cookie
return;
}
// get the role
string[] roles = authTicket.UserData.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
// Set the security context
ISecurityService security = ContainerProvider.RequestContainer.Resolve<ISecurityService>();
Models.User user = security.GetUser(authTicket.Name);
if (user == null)
{
FormsAuthentication.SignOut();
throw new HttpException((int)System.Net.HttpStatusCode.Unauthorized, "Session expired!");
}
AppIdentity id = new AppIdentity(user, !authTicket.Expired);
AppPrincipal principal = new AppPrincipal(id, roles);
Context.User = principal;
}
L'appel ContainerProvider.RequestContainer.Resolve<ISecurityService>();
est à un conteneur Autofac, mais vous pouvez faire tout ce que vous besoin de/vouloir ici.
Les classes AppIdentity
et sont personnalisées, donc je peux accéder à mes rôles, mais ils ne sont pas si compliqués.
Il n'y a pas de modèle de fournisseur de rôles dans MVC? –
Connexe: http://stackoverflow.com/questions/512922/how-do-you-handle-membership-roles-when-using-nhibernate – Cherian