问题
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");
}
回答1:
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.
回答2:
I usually avoid the go and back turn of redirection, transferring the control with Server.Transfer.
来源:https://stackoverflow.com/questions/14003940/login-redirect-issue