0

This seems like it should be so simple, yet I can't figure it out even after scouring stackoverflow.

I have a very standard Login control:

<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>

With the following as my code-behind:

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

Currently, I am always redirected to "~" even though I am logging in as a SuperAdmin. I'm sure I am checking 'IsInRole' in the wrong place, but I don't know where to do it. After I'm redirected I am indeed logged in.

This is my first "question", so hopefully I provided the proper information.

Additional information ... the code below does work:

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 Answers2

0

Is it SuperAdmin or SuperAdmins?

Have you put a break point on the conditional pieces of logic and verified that User is what you think it should be?

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

Use Authenticate event instead of LoggedIn.

Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23