Home > ASP.Net > Login Redirection based on user Role

Login Redirection based on user Role


Hi

try this in login page

   protected void Login1_LoginError(object sender, EventArgs e)
    {
        //There was a problem logging in the user

        //See if this user exists in the database
        MembershipUser userInfo = Membership.GetUser(Login1.UserName);

        if (userInfo == null)
        {
            //The user entered an invalid username...

            Login1.FailureText = "There is no user in the database with the username " + Login1.UserName;
        }
        else
        {
            //See if the user is locked out or not approved
            if (!userInfo.IsApproved)
            {

                Login1.FailureText = "Your account has not yet been approved by the site's administrators. Please try again later...";
            }
            else if (userInfo.IsLockedOut)
            {
                Login1.FailureText = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked.";
            }
            else
            {
                //The password was incorrect (don't show anything, the Login control already describes the problem)
                Login1.FailureText = string.Empty;
            }
        }

    }
    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        TextBox TextBox1 = (TextBox)Login1.FindControl("UserName");
        //MembershipUser user = Membership.GetUser(TextBox1.Text);
        MembershipUser user = Membership.GetUser(Login1.UserName);

        if (Request.QueryString["ReturnUrl"] != null)
        {
            Login1.DestinationPageUrl=Request.QueryString["ReturnUrl"].ToString();
        }
        else
        {

            //-- check if login user in Admin role
            if (Roles.IsUserInRole(TextBox1.Text, "Admin"))
            {
                Login1.DestinationPageUrl="~/Admin/Default.aspx";

            }
            //-- check if login user in User role
            else if (Roles.IsUserInRole(TextBox1.Text, "User"))
            {
                Login1.DestinationPageUrl="~/User/Default.aspx";
            }
        }
       
    }

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: