简体   繁体   中英

ASP.NET Membership Login: Redirect user after logged in

I have a web site that uses Membership and form authentication:

ASP.NET Page:

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

Code behind:

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

The problem is that Membership.GetUser() always returns null. I tried to get username with Page.User.Identity.Name but it is always empty string. Is there a way to find username of currently logged in user or even better: define after-login behavior of <asp:LoginView> or <asp:Login> ?

Edit:

MSDN: Login.LoggedIn Event Occurs when the user logs in to the Web site and has been authenticated. ( link ) So the problem is that why after authenticating user Membership.GetUser() returns null?

One of my colleagues said that instead of using Membership.GetUser() use Membership.GetUser(UserNameTextBox.Text) and it is okay because user is authenticated with this username BUT the problem is that I can not find a way to get Username.Text . I used findControl() and got error.

Any Idea?

Try with this:

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

I have always used my own authentication using form authentication.

I attached the code i wrote which deals with the user's login to the system

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

As you can see the first line i am getting the user role from the database, if the user is not exist the function returns null.

Next, if the user's account is authorized then i am registering him using the FormsAuthentication class i am passing the user's role.

Next i am setting a session and redirect the user to another page.

I hope it's helpful

Finally I found the solution based on the Idea of @LolCoder:

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

    if (Roles.IsUserInRole(username, "User"))
    {
        //...
    }
    // ...
}

My answer works!

protected void LoginUser_LoggedIn(object sender, EventArgs e)
{

      System.Web.UI.WebControls.Login controlLogin = (Login)sender;
      MembershipUser user = (MembershipUser)Membership.GetUser(controlLogin.UserName);
      ........
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM