locked
User.Identity.GetUserId(); returns a wrong value RRS feed

  • Question

  • User-973886032 posted

    Hi guys

    I am trying to get the ASPNetUsers Id in the database and I tried using 

                var user =  User.Identity.GetUserId();
    
                var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;  
                var userName = User.FindFirst(ClaimTypes.Name).Value; 

    At runtime (debug mode) user returns "adb73d0f-1c51-4650-8c8a-dbe1112cbd78"

    But in my database I have 2446f944-1e1c-4ca7-8b27-737ad79c242a

    Username above returns null

    I saw something similar here, but requires modification of the the microsoft class, which I cant find

    https://stackoverflow.com/questions/20864882/asp-net-mvc-user-identity-getuserid-returning-wrong-guid

    Whats wrong here ?

    thanks

    Ehi

    Friday, July 19, 2019 11:07 AM

Answers

  • User753101303 posted

    Hi,

    Wrong connection string or db ? Else as most source is available I would have a look to see what it does exactly ? You are using the database as well for authentication or it is a 3rd party and you store local profiles ? It can get complex as you end up in having multiple ids depending on which system you consider.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 19, 2019 11:22 AM

All replies

  • User753101303 posted

    Hi,

    Wrong connection string or db ? Else as most source is available I would have a look to see what it does exactly ? You are using the database as well for authentication or it is a 3rd party and you store local profiles ? It can get complex as you end up in having multiple ids depending on which system you consider.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 19, 2019 11:22 AM
  • User475983607 posted

    afrika

    Hi guys

    I am trying to get the ASPNetUsers Id in the database and I tried using 

                var user =  User.Identity.GetUserId();
    
                var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;  
                var userName = User.FindFirst(ClaimTypes.Name).Value; 

    At runtime (debug mode) user returns "adb73d0f-1c51-4650-8c8a-dbe1112cbd78"

    But in my database I have 2446f944-1e1c-4ca7-8b27-737ad79c242a

    Username above returns null

    I saw something similar here, but requires modification of the the microsoft class, which I cant find

    https://stackoverflow.com/questions/20864882/asp-net-mvc-user-identity-getuserid-returning-wrong-guid

    Whats wrong here ?

    thanks

    Ehi

    I recommend that you read the entire link you provided?  The original posters was connected to local DB but thought they were connected to another DB.

    Also, you other posts show you have different database yo u are trying to join[ https://forums.asp.net/p/2157835/6270678.aspx?How+do+you+enforce+PK+FK+relationships+between+2+different+DBs+

    Perhaps you introduced a bug?  Are you using the Visual Studio debugger to check your logic?

    Friday, July 19, 2019 11:46 AM
  • User-973886032 posted

    Hi,

    Wrong connection string or db ? Else as most source is available I would have a look to see what it does exactly ? You are using the database as well for authentication or it is a 3rd party and you store local profiles ? It can get complex as you end up in having multiple ids depending on which system you consider.

    I dont understand how that is possible. Here is my startup.cs

      public void ConfigureServices(IServiceCollection services)
            {
                services.Configure<CookiePolicyOptions>(options =>
                {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                });
    
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(
                        Configuration.GetConnectionString("DefaultConnection")));
                services.AddDefaultIdentity<IdentityUser>()
                        .AddDefaultUI(UIFramework.Bootstrap4)
                    .AddEntityFrameworkStores<ApplicationDbContext>();
    
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
    
    
    
    ////////////////////-----------------------and my app.json
    
    
    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-Xxxxxx-33928D3A-89A4-47C3-9CC5-1ED833938877;Trusted_Connection=True;MultipleActiveResultSets=true"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"
    }
    

    secondly it was VS that created my Identity database and connection string, so thats quite confusing

    My datacontext file has only one connection string and does not have an identity feature

    How do you check which db is being used ? 

    Friday, July 19, 2019 1:03 PM
  • User475983607 posted

    The point is, you referenced a link to prove another person is having the same issue as you.  If you read post, the user was looking at the wrong DB.

    How do you check which db is being used ? 

    You shared the connection string...

    (localdb)\\mssqllocaldb;Database=aspnet-Xxxxxx-33928D3A-89A4-47C3-9CC5-1ED833938877

    We have no idea how you verified the GUID is incorrect.  I assume you are either querying the wrong database or your query is incorrect.

    Keep in mind, Identity is well tested.  I doubt the bug is in Identity.

    Friday, July 19, 2019 1:22 PM
  • User-973886032 posted

    Keep in mind, Identity is well tested.  I doubt the bug is in Identity.

    I am not doubting that, as it has worked well in my previous projects in 2.0 and 2.2.

    About the multiple databases I earlier talked about, kindly note that only one uses asp.net core identity, the others were dbs developed by me from scratch. I am clearing my cookies, restarting my laptop and will create a fresh account and revert. ta

    Friday, July 19, 2019 1:40 PM
  • User-973886032 posted

    Hi,

    Wrong connection string or db ? Else as most source is available I would have a look to see what it does exactly ? You are using the database as well for authentication or it is a 3rd party and you store local profiles ? It can get complex as you end up in having multiple ids depending on which system you consider.

    thanks guys

    I found the problem.

    I had created a solution in VS 2019 and hoping to get asp.net core 3.0 but when I was advised in this thread below to install VS 2019 PREVIEW version instead. I then had 2 dbs with the same name, created by asp.net core, so was looking at the older db

    https://forums.asp.net/t/2157021.aspx?Cant+seem+to+get+started+with+3+0+core+IN+VS+2019

    Friday, July 19, 2019 1:51 PM