Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# get logged on user name

foreach (System.Management.ManagementObject Process in Processes.Get())
{
    if (Process["ExecutablePath"] != null && 
        System.IO.Path.GetFileName(Process["ExecutablePath"].ToString()).ToLower() == "explorer.exe" )
    {
        string[] OwnerInfo = new string[2];
        Process.InvokeMethod("GetOwner", (object[])OwnerInfo);

        Console.WriteLine(string.Format("Windows Logged-in Interactive UserName={0}", OwnerInfo[0]));

        break;
    }
}
Comment

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 :: best practice c# check if string is null or whitespace 
Csharp :: print in c# unity 
Csharp :: c# sort array string by length 
Csharp :: traversing an enum c# 
Csharp :: round double c# 
Csharp :: random seed in c# 
Csharp :: c# keep console open 
Csharp :: net use delete 
Csharp :: formula text and/or netsuite 
Csharp :: increase variable C# 
Csharp :: http error 502.5 asp.net core 2.2 
Csharp :: Unity banner ad C# 
Csharp :: asp net bootstrap 5 navigation bar 
Csharp :: c# datetimepicker set weeks after today 
Csharp :: hash password with salt c# 
Csharp :: c# average of 3 numbers 
Csharp :: c# stop loop 
Csharp :: c# list remove duplicate items 
Csharp :: how to close and reopen an app in c# 
Csharp :: decimal to string c# 
Csharp :: c# afficher texte 
Csharp :: function in Razor Pages 
Csharp :: all month in array 
Csharp :: how to deselect a button through code unity 
Csharp :: swaggergen add service not getting info in .net core 
Csharp :: how to make an object move in unity 
Csharp :: clear controls from panel c# 
Csharp :: create list c# 
Csharp :: dialog box with form flutter 
Csharp :: how to set unique constraint from EF core 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =