IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

NullReferenceException when using resource owner flow

Open henningst opened this issue 10 years ago • 5 comments

I have an IdentityServer4 where I've set up multiple clients. I have a few clients using GrantTypes.Implicit. These are used from ASP.NET MVC apps and are working fine.

I'm using in memory config for clients and scopes, but have implemented a MongoDB backed User Repository and have implemented my own IProfileService and IResourceOwnerPasswordValidator

Now I want to protect a WebAPI with IdSrv and have added a client using GrantTypes.ResourceOwnerPassword.

I have a third MVC application that should allow anonymous users, but will get data from the WebAPI protected by IdSrv. I'm using TokenClient to get a token via RequestResourceOwnerPasswordAsync as shown below:

var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("some-username", "some-password", scope);

The client validation is successful, but I'm getting a NullReferenceException because GetSubjectId is called, and since I'm authenticating as a server client, there is no subject.

[12:56:01 INF] Start token request.
[12:56:01 INF] Secret id found: my-mvc-app
[12:56:01 INF] Client validation success
[12:56:03 FTL] Unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at IdentityServer4.Extensions.PrincipalExtensions.GetSubjectId(IIdentity identity)
   at IdentityServer4.Validation.TokenRequestValidator.<ValidateResourceOwnerCredentialRequestAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityServer4.Validation.TokenRequestValidator.<RunValidationAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityServer4.Validation.TokenRequestValidator.<ValidateRequestAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityServer4.Endpoints.TokenEndpoint.<ProcessTokenRequestAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityServer4.Endpoints.TokenEndpoint.<ProcessAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at IdentityServer4.Hosting.IdentityServerMiddleware.<Invoke>d__3.MoveNext()

I'm not really sure if I'm using the correct flow and approach for this scenario. Is Resource Owner Flow the correct thing to do here?

I'm also a bit unsure if using the same IResourceOwnerPasswordValidator both for validating regular users and for server-to-server users like I'm doing now. Is this ok as long as I'm indicating in my user repository if a user is a regular user or a server user?

henningst avatar Sep 26 '16 11:09 henningst

resource owner is for humans. Server to server is client credentials grant.

leastprivilege avatar Sep 26 '16 11:09 leastprivilege

Ok, thanks! That makes sense. I have now changed to using Client Credentials flow, but I haven't been able to figure out the best practice for handling the tokens I'm receiving.

I get a token using RequestClientCredentialsAsync like this.

var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("my-api-scope");

but I'm currently getting new tokens on every request. What is the best practice for persisting the tokens on the client and making sure I get new tokens using a refresh token?

My client is configured like this:

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
            {
                AuthenticationScheme = "oidc",
                SignInScheme = "Cookies",

                Authority = "http://localhost:5010",
                RequireHttpsMetadata = false,

                ClientId = "my-mvc-app",
                ClientSecret = "this-is-my-secret",

                ResponseType = "code",
                Scope = { "my-api-scope" },

                GetClaimsFromUserInfoEndpoint = true,
                SaveTokens = true
            });

henningst avatar Sep 26 '16 11:09 henningst

To improve discoverability of frequently asked and/or more general questions, we decided to move those questions to StackOverflow:

https://stackoverflow.com/questions/tagged/?tagnames=identityserver4&sort=newest

leastprivilege avatar Sep 26 '16 11:09 leastprivilege

Posted the question here: https://stackoverflow.com/questions/39702411/best-practice-for-persisting-tokens-using-client-credentials-flow

henningst avatar Sep 26 '16 12:09 henningst

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 15 '20 14:01 lock[bot]