How to pass some more parameters to token endpoint

2019-07-12 19:27发布

问题:

i need to pass some more parameters to token endpoint as

grant_type=password&username=Alice&password=password123&peop1=value&Prop2=value

to get the token

How can i pass these and where i can get them on the server

回答1:

OAuth2 resource owner password flow defines these parameters. If you need to do something else when authenticating a user you should look into the Implicit Flow -- that way you can code a custom login screen to accept any data you need from the user.



回答2:

Solution for OWIN but you can catch the idea.

Pass parameters:

grant_type=password&username=Alice&password=password123&peop1=value&Prop2=value

Get them in your authorization server provider:

public class YourAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    ...
    public override Task ValidateTokenRequest(OAuthValidateTokenRequestContext context)
    {
        context.Request.Body.Position = 0;
        var reader = new StreamReader(context.Request.Body);
        var body = reader.ReadToEnd();                         <-- you got them all!
        return base.ValidateTokenRequest(context);
    }
}

Don't forget to pass your authorization server provider in configuration:

...
var options = new OAuthAuthorizationServerOptions
{
    Provider = new YourAuthorizationServerProvider()
};
app.UseOAuthAuthorizationServer(options);
...


回答3:

On your OWIN OAuthAuthorizationServerProvider you can catch your paramter like this :

public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
    string peop1 = context.Parameters["peop1"];

    //You can pass if you need the parameter to your GrantResourceOwnerCredentials
    context.OwinContext.Set<string>("as:peop1", peop1);
}


public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    var peop1 = context.OwinContext.Get<string>("as:peop1");