Login Redirect Issue

梦想的初衷 提交于 2019-12-12 14:19:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!