Authentification, gestion des roles
Bonjour,
j'ai une application où nous gérons une partie de notre sécurité avec les roles et les web.config :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="User.aspx">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
<location path="RoleAutorise.aspx">
<system.web>
<authorization>
<allow roles="AdministrateurPrincipal" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="RoleNonAutorise.aspx">
<system.web>
<authorization>
<allow roles="AdministrateurSecondaire" />
<deny users="*" />
</authorization>
</system.web>
</location>
</configuration> |
Notre authentification est en form
Code:
1 2 3
| <authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" defaultUrl="~/Default.aspx" name="compte" />
</authentication> |
Dans l'application, nous définissons les roles dans le global.asax dans la méthode Application_PostAuthenticateRequest, le problème est que nous utilisons une variable de session où est stocké le profil pour définir le role. Or à cet instant du cycle, la session est null, d'où problème.
En regardant le net, j'ai vu deux solutions.
La première méthode , j'utilise un TicketAuthentification.
Lorsque, je me logue :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
// Create a new ticket used for authentication
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
"arnaud", // Username associated with ticket
DateTime.Now, // Date/time issued
DateTime.Now.AddMinutes(30), // Date/time to expire
true, // "true" for a persistent user cookie
"AdministrateurPrincipal", // User-data, in this case the roles
FormsAuthentication.FormsCookiePath);// Path cookie valid for
// Encrypt the cookie using the machine key for secure transport
string encrypetedTicket = FormsAuthentication.Encrypt(ticket);
// Set the cookie's expiration time to the tickets expiration time
/*if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;*/
//
if (!FormsAuthentication.CookiesSupported)
{
//If the authentication ticket is specified not to use cookie, set it in the URL
FormsAuthentication.SetAuthCookie(encrypetedTicket, false);
}
else
{
//If the authentication ticket is specified to use a cookie,
//wrap it within a cookie.
//The default cookie name is .ASPXAUTH if not specified
//in the <forms> element in web.config
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
encrypetedTicket);
//Set the cookie's expiration time to the tickets expiration time
authCookie.Expires = ticket.Expiration;
//Set the cookie in the Response
HttpContext.Current.Response.Cookies.Add(authCookie);
}
Response.Redirect(FormsAuthentication.GetRedirectUrl("arnaud", false)); |
puis dans le global.asax, je fais :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
// Get the stored user-data, in this case, our roles
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = Thread.CurrentPrincipal = new GenericPrincipal(HttpContext.Current.User.Identity, roles);
}
}
} |
Dans la deuximème méthode
Je définis un provider de roles :
Code:
1 2 3 4 5 6
| <roleManager enabled="true" defaultProvider="DispositifRoleProvider">
<providers>
<clear />
<add name="DispositifRoleProvider" type="WebApplication1.DispositifRoleProvider" applicationName="/"/>
</providers>
</roleManager> |
Puis dans ce provider, j'override la méthode :
Code:
public override string[] GetRolesForUser(string username)
Quand je passe dans cette méthode, l'objet Session n'est toujours pas disponible. Donc soit j'utilise le même mécanisme de cookie pour le role ou un autre accès.
Mes questions et mes interrogations :
1 - quelle est la méthode à utiliser (la "best practice") ou du moins celle que vous utilisez ? Application_PostAuthenticateRequest ou définition d'un nouveau provider
2 - pour récupérer mon role, fauit-il mieux utiliser un TicketAuthentification ou un accès en base pour récuperer le profil de mon utilisateur ou autre
Par avance merci