问题
I'm trying to implement a third-party auth in my sign up page, but I can't get it to work with IdentityServer4. I have it working in my login page since the oidc client is initiating the login request. However the oidc client is not aware of a request to my registration page, so when I send an auth token back, it doesn't recognize the state and throws an exception: 'No matching state found in storage'.
I know IdentityServer4 does not officially support user registration. I've got registration set up with ASP.Net Identity, but it would be nice to be able to add a Sign Up with [Third party provider] on my registration page. Is there any way that I can get around this? Can I set the state manually and send the registration request to my identity provider? That way, if the user chooses to log in instead, then the oidc client will have a valid state. Is there any other way? Thanks.
回答1:
I finally found a solution to this problem. As I stated in my answer, the oidc client maintains state information in the local storage so that it can verify that it got the response back from the intended server. You can mimic this by generating a secure random string and saving it in localStorage. Do this before sending a request to your auth server to register a new user. The code looks like this:
const nonce = this.generateUniqueString();
const state = this.generateUniqueString();
const date = new Date();
const query = `${otherQueryOptions}&state=${state}&nonce=${nonce}`;
const authSessionData = {
authority: auth_server_url,
client_id: client_id,
created: date.getTime(),
id: state,
nonce: nonce,
redirect_uri: `your/return.url`
}
// You must prefix the key with 'oidc'
localStorage.setItem(`oidc.${authSessionData.id}`, JSON.stringify(authSessionData));
const registrationPath = `myServerRegistrationPath?${query}`
navigateToPage(registrationPath);
That fixed the problem for me.
来源:https://stackoverflow.com/questions/55546148/integrate-third-party-login-in-from-my-registration-page-with-identityserver4-an