Note: this is my first time using .NET identity and I am flying by the seat of my pants.
I am building a project with .NET identity. I defined my endpoints with Relatively early in the project I had Identity working, returning bearer tokens, and successfully authenticating and accessing secured endpoints with Swagger.
At some point the /login
endpoint stopped returning tokens, instead just returning an OK
status. So i started debugging. When I step through the /login
endpoint in Microsoft.AspNetCore.Routing.IdentityApiEndpointRouteBuilderExtensions.MapIdentityApi<>()
I noticed that /login doesn't actually return any data. There is just a comment that reads: The signInManager already produced the needed response in the form of a cookie or bearer token.
This makes no sense to me whatsoever, but clearly, it works because I had this working at some point. I assume there is something in my configuration that is messing up signInManager
's ability to return data, however, it does that.
I've tried stepping through signInManager
as it tries to sign in the user but there are thousands of lines of code and many branches the code could take. I don't know where to start. I've looked through my configuration settings, but I don't really know what half of this stuff does. I've compared my settings to what other people have done, but everyone seems to do this differently, so I can't tell what's standard, what's this guy's weird design choices, and what's irrelevant.
Here is a copy of my builder method so you can see my configuration https://pastebin.com/2z3Y5Vbj
Whats wrong? why doesn't /login
return tokens?
I think the problem is about the scheme name. When using "asp.net core identity" with endpoints. It will have two working modes when login.
Mode 1 is gererating cookie for scheme CookieAuthenticationDefaults.AuthenticationScheme
(also called "cookies").
Mode 2 is reponsed with bear token for the scheme IdentityConstants.BearerScheme
(also called "Identity.Bearer").
As you said, there have been so many ways to use asp.net core identity, but there are some differences.
AddIdentityCore<IdentityUser>()
: you have to manually AddCookie()
and AddBearer(IdentityConstants.BearerScheme)
AddIdentity<IdentityUser,IdentityRole>()
: It contains AddCookie()
AddDefaultIdentity<IdentityUser>()
: It contains AddCookie()
AddIdentityApiEndpoints
: it contains AddCookie()
and AddBearer(IdentityConstants.BearerScheme)
So After checking your code, you messed with the cookie scheme with the jwt scheme name which shouldn't work properly.