根据用户的角色,在登录时将用户重定向到页面

时间:2013-07-15 18:52:37

标签: asp.net redirect roles membership

这看起来应该很简单,但即使在精简stackoverflow之后我也无法弄明白。

我有一个非常标准的登录控件:

<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
<LayoutTemplate>
    <span class="failureNotification">
        <asp:Literal ID="FailureText" runat="server"></asp:Literal>
    </span>
    <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
         ValidationGroup="LoginUserValidationGroup"/>
    <div class="accountInfo">
        <fieldset class="login">
            <legend>Account Information</legend>
            <p>
                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:<span class="required"> *</span></asp:Label>
                <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                     CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                     ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
            </p>
            <p>
                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:<span class="required"> *</span></asp:Label>
                <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                     CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                     ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
            </p>
            <p>
                <asp:CheckBox ID="RememberMe" runat="server" />
                <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
            </p>
        </fieldset>
        <p class="submitButton">
            <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
        </p>
        <p><asp:HyperLink ID="ResetLink" runat="server" NavigateUrl="~/ResetPassword.aspx">Forgot password?</asp:HyperLink></p>
    </div>
</LayoutTemplate>

以下作为我的代码隐藏:

Protected Sub LoginUser_LoggedIn(sender As Object, e As System.EventArgs) Handles LoginUser.LoggedIn
        If User.IsInRole("SuperAdmins") Then
            Me.LoginUser.DestinationPageUrl = "~/Staff/Default.aspx"
        ElseIf User.IsInRole("Admins") Then
            Me.LoginUser.DestinationPageUrl = "~/Admin/Default.aspx"
        ElseIf User.IsInRole("Teachers") Then
            Me.LoginUser.DestinationPageUrl = "~/Teacher/Default.aspx"
        ElseIf User.IsInRole("Providers") Then
            Me.LoginUser.DestinationPageUrl = "~/Provider/Default.aspx"
        Else
            Me.LoginUser.DestinationPageUrl = "~"
        End If
    End Sub

目前,我总是被重定向到“〜”,即使我以SuperAdmin身份登录。我确定我在错误的地方检查'IsInRole',但我不知道在哪里做。在我被重定向后,我确实登录了。

这是我的第一个“问题”,所以希望我提供了适当的信息。

其他信息...... 下面的代码工作:

Protected Sub LoginUser_LoggedIn(sender As Object, e As System.EventArgs) Handles LoginUser.LoggedIn
If (Roles.IsUserInRole(LoginUser.UserName, "SuperAdmins")) Then
    Me.LoginUser.DestinationPageUrl = "~/Staff/Default.aspx"
ElseIf (Roles.IsUserInRole(LoginUser.UserName, "Admins")) Then
    Me.LoginUser.DestinationPageUrl = "~/Admin/Default.aspx"
ElseIf (Roles.IsUserInRole(LoginUser.UserName, "Teachers")) Then
    Me.LoginUser.DestinationPageUrl = "~/Teacher/Default.aspx"
ElseIf (Roles.IsUserInRole(LoginUser.UserName, "Providers")) Then
    Me.LoginUser.DestinationPageUrl = "~/Provider/Default.aspx"
Else
    Me.LoginUser.DestinationPageUrl = "~"
End If

End Sub

2 个答案:

答案 0 :(得分:0)

SuperAdmin还是SuperAdmins

您是否在条件逻辑上设置了断点,并确认User是您认为的应该是什么?

答案 1 :(得分:0)

使用Authenticate事件而不是LoggedIn。

相关问题