0
votes

Am using forms authentication along with the asp.net login controls in my website. Based on the roles i have to redirect my users to two different pages.

The code works but occasionally it takes me to either default.aspx which is not present and sometimes it simple refreshes my current login page. Any help is appreciated.

Here is my code behind

protected void Login1_LoggedIn(object sender, EventArgs e)
{
    string[] rolenames = System.Web.Security.Roles.GetRolesForUser(User.Identity.Name);
    if (rolenames.Length > 0)
    {            
        if (rolenames[0] == "Administrators")
            Response.Redirect("~/Administrators/Home.aspx");
        else if (rolenames[0] == "Employees")
            Response.Redirect("~/Employees/Home.aspx");
    }
}

and following markup from web.config

<authentication mode="Forms">
  <forms loginUrl="~/Login.aspx"
         slidingExpiration="true" 
         cookieless="AutoDetect"
         ></forms>
</authentication>
2
What if user has no roles? It will redirect to a default page then, I guess. You lack the clause to handle this branch.Wiktor Zychla

2 Answers

0
votes

You missed the default condition, what if none of the above IF condition holds true ? That case it will just refresh the current page after post back.

You forgot to mention code behind Login1_LoggedIn is on which page ?

0
votes

And What happens if The rolenames[0] is not and "Administrators" or "Employees"? Set a default condition and you should be good to go.