Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Get logged in user in ASP.Net

protected void LogIn(object sender, EventArgs e)
 {
        if (IsValid)
        {
            // Validate the user password
            var userManager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();              

            // This doen't count login failures towards account lockout
            // To enable password failures to trigger lockout, change to shouldLockout: true
            var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);

            switch (result)
            {
                case SignInStatus.Success:
              
                    //If login success find the user record by email using user manager
                    var user =  userManager.FindByEmail(Email.Text);        
                    
                    //Store logged in user in session
                    Session["CurrentUser"] = user;

                    //find out current user's role and save it in session
                    if(userManager.IsInRole(user.Id, "Admin"))
                    {
                        Session["CurrentUserRole"] = "Admin";
                    }
                    else
                    {
                        Session["CurrentUserRole"] = "Staff";
                    }

                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    break;
                case SignInStatus.LockedOut:
                    Response.Redirect("/Account/Lockout");
                    break;
                case SignInStatus.RequiresVerification:
                    Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",  Request.QueryString["ReturnUrl"],RememberMe.Checked),true);
                    break;
                case SignInStatus.Failure:
                default:
                    FailureText.Text = "Invalid login attempt";
                    ErrorMessage.Visible = true;
                    break;
            }
        }
    }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Get #logged #user
ADD COMMENT
Topic
Name
4+6 =