我有一个使用会员资格和表单身份验证的网站:
ASP.NET 页面:
<asp:LoginView
ID="HeadLoginView" ...
<AnonymousTemplate>
<asp:Login ID="LoginUser" OnLoggedIn="LoginUser_LoggedIn" ... >
<LayoutTemplate>
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:CheckBox ID="RememberMe" runat="server"/>
<asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Remember me.</asp:Label>
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Login" style="width:100%; height:35px"/>
</LayoutTemplate>
</asp:Login>
</AnonymousTemplate>
...
</asp:LoginView>
后面的代码:
protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
MembershipUser user = Membership.GetUser();
if (Roles.IsUserInRole(user.UserName, "User"))
{
// Do something and redirect to user page...
}
if (Roles.IsUserInRole(user.UserName, "Admin"))
{
// Do something and redirect to admin page...
}
}
问题是Membership.GetUser()
总是返回null。我试图获取用户名,Page.User.Identity.Name
但它始终是空字符串。有没有办法找到当前登录用户的用户名,甚至更好:定义<asp:LoginView>
or的登录后行为<asp:Login>
?
编辑:
MSDN:(Login.LoggedIn Event Occurs when the user logs in to the Web site and has been authenticated.
链接)所以问题是为什么在验证用户 Membership.GetUser() 后返回 null?
我的一位同事说,不要使用Membership.GetUser()
use Membership.GetUser(UserNameTextBox.Text)
,这没关系,因为用户已使用此用户名进行身份验证,但问题是我找不到获取方法Username.Text
。我使用findControl()
并得到错误。
任何的想法?