Hi,
   I have a small problem in Form based authentication. 
I have custom roles defined in the database. I get the roles from DB and then store in 
the Cookie as shown in the code below for assigning it later to the current context 
user.

private void Submit_Click(object sender, System.EventArgs e)
                {  
                        StringBuilder strRoles= new StringBuilder();
                        // Get roles from UserRoles table, and add to cookie
                        RoleDB oRole = new RoleDB();
                        // GetRoles First Authenticates and then get roles. I have 
removed all the error handling code for ease.
                        SqlDataReader roles = 
oRole.GetRoles(txtUserId.Text,txtPwd.Text);
                        while(roles.Read())
                        {
                                strRoles.Append(roles.GetString(0)+";");
                        }
                        HttpCookie authCookie = new  HttpCookie("Portal");
                        FormsAuthenticationTicket authTkt ;
                        string strMyRoles =strRoles.ToString();
                        authTkt = new FormsAuthenticationTicket(1, txtUserId.Text, 
DateTime.Now, DateTime.Now.AddMinutes(20), false, strMyRoles);
                        authCookie.Value = FormsAuthentication.Encrypt(authTkt);
                        Response.Cookies.Add(authCookie);
                        
FormsAuthentication.RedirectFromLoginPage(txtUserId.Text,false,"/");
                }
        

But when I try to access the userData part (strMyRoles) in the cookie after decrypting 
the FormsAuthentication Ticket, it shows it as null. All the other information like 
Name, Settime, Expiration time etc are available except for this .
Snippet of the code where I am accessing the roles and assigning it to the current 
context
                                        
                // Get roles from roles cookie
                HttpCookie oRoleCookie = Context.Request.Cookies["Portal"];
                FormsAuthenticationTicket ticket = 
FormsAuthentication.Decrypt(oRoleCookie.Value);

                //convert the string representation of the role data into a string 
array
                ArrayList userRoles = new ArrayList();
                foreach (String role in ticket.Name.Split( new char[] {';'} )) 
                {
                        userRoles.Add(role);
                }
                String[] roles = (String[]) userRoles.ToArray(typeof(String));
                // Add our own custom principal to the request containing the roles in 
the auth ticket
                Context.User = new GenericPrincipal(Context.User.Identity, roles);

Can somebody help me in resolving this problem.

Thanks
PrashanthG

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to