IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Sometimes get a "idp claim is missing" with AspNetIdentity when authorizing

Open liamdawson opened this issue 10 years ago • 42 comments

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.

liamdawson avatar Sep 09 '16 05:09 liamdawson

How do you sign in the user?

leastprivilege avatar Sep 09 '16 05:09 leastprivilege

Via a login endpoint. In this instance, the idp claim issue arose after automatically being accepted as being logged in (via cookies).

liamdawson avatar Sep 09 '16 06:09 liamdawson

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)

liamdawson avatar Sep 09 '16 06:09 liamdawson

I see - so it maybe happens when the gets "slided" ? @brockallen ?

leastprivilege avatar Sep 09 '16 06:09 leastprivilege

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.

kdaveid avatar Sep 09 '16 07:09 kdaveid

Possibly. I did have a reminder for myself to look into this.

brockallen avatar Sep 09 '16 13:09 brockallen

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.

ManuelRauber avatar Sep 10 '16 07:09 ManuelRauber

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!

brockallen avatar Sep 11 '16 16:09 brockallen

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.

brockallen avatar Sep 11 '16 16:09 brockallen

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)

liamdawson avatar Sep 11 '16 23:09 liamdawson

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.

brockallen avatar Sep 11 '16 23:09 brockallen

Unfortunately, I haven't found a consistent repro. Haven't made any changes to the user, though, at all.

liamdawson avatar Sep 12 '16 00:09 liamdawson

To test this I did these things:

  1. 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.

  2. 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.

brockallen avatar Sep 12 '16 00:09 brockallen

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)

takazm avatar Sep 12 '16 12:09 takazm

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

damienbod avatar Sep 13 '16 19:09 damienbod

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.

brockallen avatar Sep 14 '16 19:09 brockallen

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.

brockallen avatar Sep 14 '16 20:09 brockallen

[remove]

hmheng avatar Sep 15 '16 04:09 hmheng

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.

kdaveid avatar Sep 15 '16 09:09 kdaveid

I have updated and done some initial tests. I cannot reproduce this problem anymore.

Greetings Damien

damienbod avatar Sep 15 '16 11:09 damienbod