Sometimes get a "idp claim is missing" with AspNetIdentity when authorizing
System.InvalidOperationException: idp claim is missing
at IdentityServer4.Extensions.PrincipalExtensions.GetIdentityProvider(IIdentity identity) in IdentityServer4-1.0.0-rc1\src\IdentityServer4\Extensions\PrincipalExtensions.cs:line 184} System.InvalidOperationException
IS4 entries in project.json:
"IdentityServer4": "1.0.0-rc1",
"IdentityServer4.AspNetIdentity": "1.0.0-rc1",
"IdentityServer4.AccessTokenValidation": "1.0.0-rc1" (resolving to 1.0.1-beta1)
I'm using ASP.Net Core Identity, and I've included the IdentityServer4 token validation library (I want to secure an api for user creation etc as part of the same service).
Here are the claims that are set on the current identity:
[0]: "sub: 3b26b2f3-ed9d-4ca0-9fd1-cb686484002d"
[1]: "name: [email protected]"
[2]: "AspNet.Identity.SecurityStamp: eccf3972-e976-450d-a5a2-3be969616786"
[3]: "role: sysadmin"
[4]: "mp.sysadmin: true"
[5]: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: Liam"
[6]: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname: Dawson"
Is this just a "we should allow returning null for the provider"? Or is there a further underlying issue/misconfiguration? In this instance, I'm only supporting one IDP, so I don't personally mind a bandaid fix.
How do you sign in the user?
Via a login endpoint. In this instance, the idp claim issue arose after automatically being accepted as being logged in (via cookies).
Entirety of the login endpoint:
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model, string redirectUrl = null)
{
if (!ModelState.IsValid)
{
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage ?? e.Exception?.Message);
_logger.LogWarning("Login request was invalid. Errors: {errors}, Password provided for {username}: {password_provided}.",
errors,
model.Email,
!string.IsNullOrEmpty(model.Password));
return StatusCode(StatusCodes.Status400BadRequest);
}
var result = await _signInManager.PasswordSignInAsync(model.Email,
model.Password,
model.RememberMe ?? false,
true);
if (result.Succeeded)
{
return Redirect(string.IsNullOrEmpty(redirectUrl) ? "/" : redirectUrl);
}
return StatusCode(StatusCodes.Status403Forbidden);
}
(which wasn't called in this instance, because already logged in)
I see - so it maybe happens when the gets "slided" ? @brockallen ?
I have made the same experiences. Usually this happens when (in dev) I restart the auth server (IdentityServer4) but not the MVC client and make a new request to an [Authorize]'d method within MVC app. The exact behavior I'll probably see today as I'll go further with implementing.
Possibly. I did have a reminder for myself to look into this.
I can confirm this behavior using ASP.NET Core Identity, too.
SPA with Implicit using the popup mode. Code is the same as in your quickstarter examples.
The cookie does not lose any claims when it slides, but it will lose claims if the security stamp is invalidated. Is this perhaps what you're running into?
If any of you having this issue can confirm that it's not the cookie sliding, but instead the security stamp validation I'd appreciate it. Thanks!
BTW, the properties are not lost so what we'll have to do is either 1) store these crucial claims in the properties, and then reload the claims from the properties, or, the less desirable option, 2) in our AddAspNetIdentity we replace the security stamp validator with all of the exact same logic as the built-in one, but instead we reload from the current claims rather than from the DB.
security stamp validation
How exactly is this validation performed? As in, is it ASP.Net identity related, and could it happen due to a server restart (I haven't explicitly set any secrets or anything about Identity)
It's automatic within ASP.NET Identity. When anything related to the user's authentication changes (password, 2fa settings, email settings, etc). You can tell by the security stamp claim changing in the user's cookie.
If you can't tell, then can you narrow down the exact repro steps so I can make sure what I'm seeing matches what you're seeing? Thanks.
Unfortunately, I haven't found a consistent repro. Haven't made any changes to the user, though, at all.
To test this I did these things:
-
set the cookie to be sliding and set the expiration to be low so i could see when the cookie slid. when the cookie slides i see all the same claims, so that's why i'm not thinking it's that.
-
login and then change the user's 2fa settings (or change password, or something else to affect the security stamp). then i saw that the claims issued at login time as lost (including idp). since the cookie renewal is done within the cookie middleware (and not from the mvc code) then we don't have a chance to augment the claims being added so you lose the crucial claims issues issued at at login time (they just reload claims from the ASP.NET Identity DB). that's why i opened the issue in the ASP.NET Identity repo, as this is not ideal behavior IMO.
I am getting the same exception idsvr aspnet core, client aspnet core. Also happens when auto authenticated through cookie.
Unhandled exception: System.InvalidOperationException: idp claim is missing .....
at IdentityServer4.Extensions.PrincipalExtensions.GetIdentityProvider(IIdentity identity)
I have this problem as well, OpenID Connect Implicit Flow.
https://github.com/damienbod/AspNet5IdentityServerAngularImplicitFlow
I can reproduce it by logging on directly to IdentityServer4, and then starting the SPA and the Implicit Flow login request causes this problem.
Greetings Damien
Ok, I now understand better what's happening. ASP.NET Identity always reloads the user's claims from the DB every 15m by default. This is done by the security stamp validator, but that behavior is odd given that it seems orthogonal the intended purpose of the security stamp validator.
Anyway, I am now very confident that this is the reason behind this issue. I'll keep everyone posted on what we're going to do.
Ok, I've added a custom security stamp validator to not reload claims from the DB. IdentityServer4.AspNetIdentity 1.0.0-rc1-update2 has been released to NuGet. Please test it and let us know how it's working.
[remove]
Ok, so there is what I experienced so far. Before 1.0.0-rc1-update2 the missing idp issue occurred when I signed out of the client (MVC app) but the idsvr was offline and could not receive the redirect/offline request.
Sign out in mvc:
await HttpContext.Authentication.SignOutAsync("Cookies");
await HttpContext.Authentication.SignOutAsync("oidc");
When I then started an instance of idsvr and reloaded the mvc page or tried to get to an secured page ([Authorized]) it would throw the System.InvalidOperationException: idp claim is missing.
Doing these steps is probably not the use case and I haven't digged into the whole problem so I don't know if this is by design or just me being inconsistent in (re-)starting servers...
Anyway, with 1.0.0-rc1-update2 this does not happen anymore.
I have updated and done some initial tests. I cannot reproduce this problem anymore.
Greetings Damien