I want to re-direct people logging in based on their role. When I use the code below, the roles are coming up empty when debugging - I suspect because they haven't been completely logged in yet? I may be tapping into the the wrong event, can someone point me in the right direction?
<asp:Login ID="LoginUser" OnLoggedIn="Login1_LoggedIn" runat="server"
DestinationPageUrl="~/Login.aspx" EnableViewState="false"
RenderOuterTable="false">
<p>
<asp:Button ID="LoginButton" CssClass="submitButton" runat="server" Width="70"
CommandName="Login" Text="Log In"
ValidationGroup="LoginUserValidationGroup" />
</p>
</asp:login>
protected void Login1_LoggedIn(object sender, System.EventArgs e)
{
// Overrides ReturnUrl page parameter
//Response.Redirect(LoginUser.DestinationPageUrl);
if (User.IsInRole("Member"))
Response.Redirect("~/AskExpert/AskQuestion.aspx");
else if (User.IsInRole("Expert"))
Response.Redirect("~/Admin/Experts/ViewQuestions.aspx");
else if (User.IsInRole("Admin"))
Response.Redirect("~/Admin/AdminHome.aspx");
}
You could use Roles.IsUserInRole() instead. User.IsInRole reads the authentication data from the auth cookie and it seems that this data is not yet set when that event is fired.
if (Roles.IsUserInRole("Member"))
Response.Redirect("~/AskExpert/AskQuestion.aspx");
else if (Roles.IsUserInRole("Expert"))
Response.Redirect("~/Admin/Experts/ViewQuestions.aspx");
else if (Roles.IsUserInRole("Admin"))
Response.Redirect("~/Admin/AdminHome.aspx");
The downside to Roles.IsUserInRole is that if you're using a database provider, it results in a roundtrip.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With