这似乎应该很简单,但即使在搜索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
目前,即使我以超级管理员身份登录,我也总是被重定向到“~”。我确定我在错误的地方检查了“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
结束子