How to pass some more parameters to token endpoint web api 2

吃可爱长大的小学妹 提交于 2019-12-24 03:26:46

问题


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");


来源:https://stackoverflow.com/questions/22667785/how-to-pass-some-more-parameters-to-token-endpoint-web-api-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!