Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Csharp :: C# how to know if number is even or odd 
Csharp :: lwjgl fullscreen 
Csharp :: Test for even Number 
Csharp :: how to count letters in c# 
Csharp :: c# copy bidimensional array 
Csharp :: disable version header c# 
Csharp :: factorial of any number 
Csharp :: C# random multiple of 5 in range 
Csharp :: check if element in hashset c# 
Csharp :: self referencing loop detected for property entity framework 
Csharp :: c# check that value is not null or 0 
Csharp :: c# do while or 
Csharp :: c# external execute batch 
Csharp :: unity camera.main.screentoworldpoint(input.mouseposition) not working 
Csharp :: c# for loops 
Csharp :: elasticsearch nested aggregation in c# 
Csharp :: c# console.writeline 
Csharp :: how to stop a coroutine unity c# 
Csharp :: matrix transpose 
Csharp :: c# dapper execute stored procedure with parameters 
Csharp :: c# webclient vs httpclient 
Csharp :: Storing Data within your TileEntity 
Csharp :: csharp attributes as generics constraints 
Csharp :: list equals in order c# 
Csharp :: C# print all properties of an object including children objects 
Csharp :: how to hide cell in epplus 
Csharp :: unity set dictionary value 
Csharp :: c# param exception 
Csharp :: how to populate a collection c# 
Csharp :: mysql executeScalar only if successful 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =