locked
Identify user details after they have logged in RRS feed

  • Question

  • User-1664485818 posted

    Hi, the code below works ok, just one issue, the code confirms if user logged in is Admin or not i.e. the code checks if the user name is within the AspNetUser table and returns a Boolean true or false.

    But I also need the user GarageID to be returned, the GarageID field is held within the AspNetUser table any ideas how to do this?

            private bool IsUserAdmin()
            {
                System.Security.Principal.WindowsIdentity identity = Context.Request.LogonUserIdentity;
               
                string loginName = identity.Name;
                var garageID = identity.User;
               
                TyrescannerWebApp.IdentityModel.tyrescannerdatabaseEntities dbcontext = new TyrescannerWebApp.IdentityModel.tyrescannerdatabaseEntities();
    
                bool val1 = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
    
                return dbcontext.AspNetUsers.Any(a => a.UserName == loginName);
             
            }

    Tuesday, December 29, 2015 8:46 AM

Answers

  • User614698185 posted

    Hi brucey,

    brucey

    var garageID = identity.User;

    If you want to get User id, you could modify your code like below:

    string garageID = User.Identity.GetUserId();
    ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == garageID);

    GetUserId() is an extension method on Identity and it is in Microsoft.AspNet.Identity.IdentityExtensions. Make sure you have added the namespace:

    using Microsoft.AspNet.Identity;

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 31, 2015 5:24 AM