2

我有一个使用会员资格和表单身份验证的网站:

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()并得到错误。

任何的想法?

4

4 回答 4

2

试试这个:

protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
    string userName = (LoginUser.FindControl(“UserName”) as TextBox).Text;
    //// or use 
    ////  string userName = (e.Item.FindControl("UserName") as TextBox).Text;
    MembershipUser user = Membership.GetUser(userName);

    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...
    }
}
于 2012-09-16T17:06:08.730 回答
0

我一直使用表单身份验证来使用自己的身份验证。

我附上了我编写的处理用户登录系统的代码

 string role = system.CheckAdminLogin(txtUserName.Text, txtPassword.Text);
    if (role == Role.admin.ToString() || role == Role.manager.ToString())
    {
        Users _user = _users.GetUserByUserName(txtUserName.Text);
        if (_user.Mode)
        {
            FormsAuthentication.SetAuthCookie(role, false);
            Session.Add("UserName", txtUserName.Text);
            Session.Add("UserID", _user.ID);
            Response.Redirect("System/ShowActivities.aspx");
        }
        else
        {
            lblLoginFail.Text = "your account is not authorized!";
            lblLoginFail.Visible = true;
        }
    }
    else
    {
        if (txtPassword.Text != "" && txtUserName.Text != "")
        {
            lblLoginFail.Text = Application["Wrong_login_data"].ToString();
            lblLoginFail.Visible = true;
        }
    }

正如您所看到的第一行,我从数据库中获取用户角色,如果用户不存在,则函数返回 null。

接下来,如果用户的帐户被授权,那么我正在使用 FormsAuthentication 类注册他,我正在传递用户的角色。

接下来我设置一个会话并将用户重定向到另一个页面。

我希望它有帮助

于 2012-09-16T17:09:31.117 回答
0

最后我根据@LolCoder的想法找到了解决方案:

protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
    String username = (HeadLoginView.FindControl("LoginUser") as Login).UserName;

    if (Roles.IsUserInRole(username, "User"))
    {
        //...
    }
    // ...
}
于 2012-09-18T08:13:43.053 回答
-1

我的回答有效!

protected void LoginUser_LoggedIn(object sender, EventArgs e)
{

      System.Web.UI.WebControls.Login controlLogin = (Login)sender;
      MembershipUser user = (MembershipUser)Membership.GetUser(controlLogin.UserName);
      ........
 }
于 2013-05-31T00:25:07.650 回答