J'essaie de mettre en place une application web sécurisée par un formulaire d'authentification. Mais je rencontre un problème pour décrypter les rôles savegardés dans mon cookie d'authentification.
Voici mon fichier web.config :
<
authenticationmode="Forms"><
formsname="AuthCookie"loginUrl="login.aspx?fonction=connect"protection="Encryption"slidingExpiration="true"timeout="10"path="/"></forms></
authentication>-------------------------------------------------------------------------------------------------------------------------------------------------
La page login.aspx possède un contrôle d'authentification. Une fois authentifié je sauvegarde les informations utilisateur (user Id & roles) dans un cookie :
string
[] roles = null;string roles_a_plat = "";string separateur_roles = @"|";AbbottRoleProvider arp = newAbbottRoleProvider();roles = arp.GetRolesForUser(providerTest.UserName.ToUpper());
foreach (string roletest in roles){
if (roles_a_plat == "") {
roles_a_plat = roletest;
}
else {
roles_a_plat += separateur_roles + roletest;
}
}
// Creation du ticket d'authentificationFormsAuthenticationTicket authTicket = newFormsAuthenticationTicket(1,
// versionproviderTest.UserName.ToUpper(),
// user nameDateTime.Now, // creationDateTime.Now.AddMinutes(10),// Expirationtrue, // Persistentroles_a_plat,
"/"); // User data// Cryptage du ticket.string encryptedTicket = FormsAuthentication.Encrypt(authTicket);// Create du cookie et ajout du ticket CryptéHttpCookie authCookie = newHttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);// Add the cookie to the outgoing cookies collection. Response.Cookies.Add(authCookie);
Lorsque j'éxecute le code via le debugger j'ai la variable roles_a_plat = "USER|ADMIN" / la variable providerTest.Username = "marqucx"
encryptedTicket ="
84D16FFA989B09F36DB9DB0E5476111B1C4798D482A277120F80D6FD287DFE3B7CFEB534AE30C20A322C6BB01BF4A447F471B3968B5F8E09696275C7EC2207004AF9A0FF242148C92D86DAFD9ADE248E
"
---------------------------------------------------------------------------------------------------------------------------------------------
J'ai modifié le fichier Global.asax de manière à intercepter les authentifications de l'application :
protectedvoid Application_AuthenticateRequest(Object sender, EventArgs e){
// Extract the forms authentication cookiestring cookieName = FormsAuthentication.FormsCookieName;HttpCookie authCookie = Context.Request.Cookies[cookieName];string[] roles = null;if (null == authCookie){
// There is no authentication cookie.return;}
FormsAuthenticationTicket authTicket = null;try{
authTicket =
FormsAuthentication.Decrypt(authCookie.Value);int intVersion = authTicket.Version;roles = authTicket.UserData.Split(
newchar[] { '|' });}
catch(Exception ex){
// Log exception details (omitted for simplicity)return;}
if (null == authTicket){
// Cookie failed to decrypt.return;}
// Create an Identity objectFormsIdentity id = newFormsIdentity(authTicket);}
-----------------------------------------------------------------------------------------------------------------
Bizarrement la variable intVersion = 2 alors que ça a été crée en version 1.
La variable authTicket.UserData = "". ON ne récupère pas les rôles sauvegardés dans le cookie.
la variable authTicket.Name = "marqucx". id.Name = "marqucx".
Je ne comprends pas pourquoi il a changé de version et pourquoi on sauvegarde le user mais pas les rôles.
Carlos