ASP.NET 5 MVC6 User.GetUserId() returns incorrect id

Pawel Maga

I have simple project in ASP.NET 5 with one registered user. I have tried to get id of current logged user by extension method GetUserId() from using System.Security.Claims namespace. Unfortunately this method returns me not existing id and i don't know why.

Here is my simple code:

var currentUserId = User.GetUserId();

Method result:

enter image description here

Database:

enter image description here

matt.

More than likely you're getting an old auth ticket that is still valid OR your getting an authentication ticket for another site your running locally. I would clear the browser cache and cookies and run it again.

Also, Try changing the name of the cookie, by default it is set to .AspNet.Cookies which can cause issues if you're running multiple apps locally. This caught me up a while back.

The cookie name can be changed like this

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",
});

The MSDN documentation on it can be found here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ASP.NET 5, MVC6, WebAPI -> ModelState.IsValid always returns true

From Dev

Request BinaryRead in ASP.NET 5 (MVC6)

From Dev

HTTP Error 403.14 in ASP.NET5 MVC6

From Dev

project.json in ASP.NET 5 and MVC6

From Dev

@Ajax.ActionLink in ASP.NET 5 MVC6

From Dev

ASP.NET 5 (MVC6) How to seed users

From Dev

How do you stub User.Identity.GetUserId() in ASP MVC 5 (Microsoft.AspNet.Identity)

From Dev

Call User.Identity.GetUserId() from Helpers with ASP.NET MVC

From Dev

ASP.NET MVC User.Identity.GetUserId() returning wrong GUID

From Dev

ASP.NET MVC User.Identity.GetUserId() returning wrong GUID

From Dev

VB.NET support for ASP.NET 5 (MVC6)

From Dev

ASP.NET 5 MVC6 Angular 2 (Beta 0) - duplicate promise

From Dev

How to self-host ASP.NET 5 MVC6 application

From Dev

@helper directive no longer works out of the box in ASP.NET5 MVC6 beta4.

From Dev

ASP.NET 5 MVC6 Custom CSS & Javascript placing convention

From Dev

Web.config gone in Asp.net 5 mvc6, how about non web config?

From Dev

How can add controller in ASP.NET MVC6 beta5 project?

From Dev

asp.net 5 (mvc6 configuration) not reading from appsettings.json

From Dev

App_Data directory in ASP.NET5 MVC6

From Dev

What to use instead of WebViewPage.RenderPage method in ASP.NET 5 MVC6

From Dev

How to use active Directory for ASP.Net 5 (MVC6) Intranet application

From Dev

How to generate a view from a controller in VS 2015 ASP.NET 5 MVC6

From Dev

ASP.NET 5 MVC6 Error: project is not a web project

From Dev

ASP.Net 5 MVC6 select taghelper produces wrong output

From Dev

ASP.NET 5 (MVC6) EF7 Foreign Key may cause cycles

From Dev

Accssing ASP.NET 5 / MVC6 Identity Custom Profile data properties

From Dev

Strange behaviour between tag-helpers and Route attribute in asp.net 5 MVC6

From Dev

How can add controller in ASP.NET MVC6 beta5 project?

From Dev

Can I add WebApi to an ASP.NET 5 (vnext) project without MVC6?

Related Related

  1. 1

    ASP.NET 5, MVC6, WebAPI -> ModelState.IsValid always returns true

  2. 2

    Request BinaryRead in ASP.NET 5 (MVC6)

  3. 3

    HTTP Error 403.14 in ASP.NET5 MVC6

  4. 4

    project.json in ASP.NET 5 and MVC6

  5. 5

    @Ajax.ActionLink in ASP.NET 5 MVC6

  6. 6

    ASP.NET 5 (MVC6) How to seed users

  7. 7

    How do you stub User.Identity.GetUserId() in ASP MVC 5 (Microsoft.AspNet.Identity)

  8. 8

    Call User.Identity.GetUserId() from Helpers with ASP.NET MVC

  9. 9

    ASP.NET MVC User.Identity.GetUserId() returning wrong GUID

  10. 10

    ASP.NET MVC User.Identity.GetUserId() returning wrong GUID

  11. 11

    VB.NET support for ASP.NET 5 (MVC6)

  12. 12

    ASP.NET 5 MVC6 Angular 2 (Beta 0) - duplicate promise

  13. 13

    How to self-host ASP.NET 5 MVC6 application

  14. 14

    @helper directive no longer works out of the box in ASP.NET5 MVC6 beta4.

  15. 15

    ASP.NET 5 MVC6 Custom CSS & Javascript placing convention

  16. 16

    Web.config gone in Asp.net 5 mvc6, how about non web config?

  17. 17

    How can add controller in ASP.NET MVC6 beta5 project?

  18. 18

    asp.net 5 (mvc6 configuration) not reading from appsettings.json

  19. 19

    App_Data directory in ASP.NET5 MVC6

  20. 20

    What to use instead of WebViewPage.RenderPage method in ASP.NET 5 MVC6

  21. 21

    How to use active Directory for ASP.Net 5 (MVC6) Intranet application

  22. 22

    How to generate a view from a controller in VS 2015 ASP.NET 5 MVC6

  23. 23

    ASP.NET 5 MVC6 Error: project is not a web project

  24. 24

    ASP.Net 5 MVC6 select taghelper produces wrong output

  25. 25

    ASP.NET 5 (MVC6) EF7 Foreign Key may cause cycles

  26. 26

    Accssing ASP.NET 5 / MVC6 Identity Custom Profile data properties

  27. 27

    Strange behaviour between tag-helpers and Route attribute in asp.net 5 MVC6

  28. 28

    How can add controller in ASP.NET MVC6 beta5 project?

  29. 29

    Can I add WebApi to an ASP.NET 5 (vnext) project without MVC6?

HotTag

Archive