locked
Identity User( Find Current Logged in User) Windows Forms RRS feed

  • Question

  • I have implemented dummy code for logging in to a windows Forms Application using Identity (Owin Auth). part of my code is as follows:

    private bool validateuser()
    {
                
        bool isAuthorised = false;
        Login1 log = new Login1();
        if (DialogResult.OK == log.ShowDialog())
        {
            Splasher.Show(typeof(frmSplash));
            userC = log.usernameTextBox.Text;
            PassC = log.passwordTextBox.Text;
                   
            if (userC == "" || PassC == "")
            {
                count++;
                //isAuthorised = false;
                if (count > 0) log.LoginStatus.Text = "Empty passwords or usernames are not allowed. please try again";                    
                if (count == 2) MessageBox.Show("You have one try remaining");
                if (count == 3) { MessageBox.Show("You have exosted your tries application will now close"); Application.Exit(); }
                validateuser();
             }
             else
             {
                 var userStore = new UserStore<IdentityUser>();
                 var userManager = new UserManager<IdentityUser>(userStore);
                 var user = userManager.Find(userC, PassC);
                 if (user != null)
                  {
                     var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                      isAuthorised = userIdentity.IsAuthenticated;
    
                      userIdentity.AddClaim(new Claim("FullName", user.UserName));
                      userIdentity.AddClaim(new Claim("User_Id", user.Id));
                      return isAuthorised;                        
                   }
                   else
                   {
                      count++;
                      //Application.Exit();
                      if (count > 0) log.LoginStatus.Text = "You may have entered a wrong password or username. Please try again";                        
                      if (count == 2) MessageBox.Show("You have one try remaining");
                      if (count == 3) { MessageBox.Show("You have exosted your tries, application will now close"); Application.Exit(); }
                      validateuser();
                    }
                  }
              } 
              count = 0;
              return isAuthorised;
    }

    When the code works fine and is able to authenticate from the database. In the current form, I'm able to get the current user by using user.UserName or id by using user.Id

    How do I get to retrieve the name or Id of the current logged in user from another form. Please Help

    Thanks


    My ASP

    Monday, March 30, 2015 6:01 PM

Answers

  • Hi,

    >>How do I get to retrieve the name or Id of the current logged in user from another form. Please Help

    About how to get the current logged user name, please try the following code

     string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Wednesday, April 8, 2015 2:14 AM
    • Marked as answer by Ronworld Monday, April 13, 2015 12:14 PM
    Wednesday, April 8, 2015 2:14 AM

All replies

  • Hi

    Perhaps, you could use this:

    Thread.CurrentPrincipal.Identity

    like:

    var claimsIdentity = Thread.CurrentPrincipal.Identity;
    var name = claimsIdentity.Claims.FirstOrDefault(c => c.Type.Equals("username", StringComparison.CurrentCultureIgnoreCase));

    Thanks

    Aram

    Monday, March 30, 2015 8:40 PM
  • Type in c.Type is giving an error. could I be missing any important reference?


    My ASP

    Saturday, April 4, 2015 10:09 AM
  • Hi,

    >>How do I get to retrieve the name or Id of the current logged in user from another form. Please Help

    About how to get the current logged user name, please try the following code

     string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Wednesday, April 8, 2015 2:14 AM
    • Marked as answer by Ronworld Monday, April 13, 2015 12:14 PM
    Wednesday, April 8, 2015 2:14 AM
  • Type in c.Type is giving an error. could I be missing any important reference?


    My ASP

    Hi,

    Looks from Aram's code, I found a blog from Leandro Boffi.

    http://leandrob.com/2012/02/claims-identity-c-4-0-dynamics/

    Some similar code like Aram's. You should install Windows Identity Foundation and the companion WIF SDK

    Best regards,

    Kristin

    .


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 8, 2015 2:34 AM
  • Type in c.Type is giving an error. could I be missing any important reference?


    My ASP

    Hi

    You will need Microsoft.IdentityModel in WIF package, you can get it from Nuget:

    https://www.nuget.org/packages/Microsoft.IdentityModel/

    Regards

    Aram


    Aram Koukia

    Sunday, April 12, 2015 8:02 PM