owin oauth send additional parameters

前端 未结 1 1012
南笙
南笙 2020-12-29 05:29

I\'m sure this is possible but not certain how to achieve. I have an OWIN OAUTH implementation that currently accepts the users Username and Password and authenticates them

相关标签:
1条回答
  • 2020-12-29 06:18

    You have to implement ValidateClientAuthentication if you haven't done so.

    This is the place where you should validate your client. In this methods you will do some sort of validation of your client and set the objects/variable which can be read in GrantResourceOwnerCredentials.

    Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    

    receives an OAuthValidateClientAuthenticationContext which contains the additional field(s) you're passing when POSTing to your authorization server.

    enter image description here

    In the picture above I've added an extra parameter uid in 4th position.

    Before you validate your context:

    context.Validated(clientId);
    

    you can set your variable/object:

    string uid = context.Parameters.Where(f => f.Key == "uid").Select(f => f.Value).SingleOrDefault()[0];
    context.OwinContext.Set<string>("SmartCard", uid);
    

    Now, in your GrantResourceOwnerCredentials you can simply read the value and use it:

    string uid = context.OwinContext.Get<string>("SmartCard");
    

    If you want to find out more you can have a look at this github repository where I pass an object:

    context.OwinContext.Set<ApplicationClient>("oauth:client", client);
    

    If you download the whole solution you can test it with a javascript/jquery client.

    UPDATE:

    You would pass an additional parameter (IE: uid) in your http POST request:

    Vanilla Js:

    var request = new XMLHttpRequest();
    request.open('POST', oAuth.AuthorizationServer, true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
    request.setRequestHeader('Accept', 'application/json');
    request.send("username=John&password=Smith&grant_type=password&uid=b17ac911-4cf1-4a3e-84a9-beac7b9da157");
    

    jQuery:

    $.ajax({
            type: 'POST',
            url: oAuth.AuthorizationServer,
            data: { username: 'John', password: 'Smith', grant_type: 'password', uid: 'b17ac911-4cf1-4a3e-84a9-beac7b9da157' },
            dataType: "json",
            contentType: 'application/x-www-form-urlencoded; charset=utf-8',
            xhrFields: {
            withCredentials: true
        },
            // crossDomain: true,
            headers: {
                    'Authorization': 'Basic ' + authorizationBasic
        }
    });
    
    0 讨论(0)
提交回复
热议问题