ASP.NET 5 MVC6 User.GetUserId () returns invalid identifier

I have a simple project in ASP.NET 5 with one registered user. I tried to get the ID of the currently logged in user using the extension method GetUserId()from using System.Security.Claimsthe namespace. Unfortunately, this method returns me a non-existing identifier, and I do not know why.

Here is my simple code:

var currentUserId = User.GetUserId();

Method Result:

enter image description here

Database:

enter image description here

+4
source share
1 answer

, , , , . cookie .

, cookie, .AspNet.Cookies, , . .

cookie :

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(0),
            regenerateIdentity: (manager, user) => manager.GenerateUserIdentityAsync(user))
    },
    CookieName = "myAuthCookie",
});

MSDN .

+3

Source: https://habr.com/ru/post/1614049/


All Articles