Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owin, GrantResourceOwnerCredentials send custom parameters

I have a Web Api where I use Owin Token Authentication, as you know you have this method for authentication by default

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
           //here you get the context.UserName and context.Password
           // and validates the user
        }

This is the JavaScript call

$.ajax({
            type: 'POST',
            url: Helper.ApiUrl() + '/token',
            data: { grant_type: 'password', username: UserName, password: Password },
            success: function (result) {
                Helper.TokenKey(result.access_token);
                Helper.UserName(result.userName);           
            },
            error: function (result) {
                Helper.HandleError(result);
            }
        });

This is perfect but the problem is that I have a multicustomer database and I have to send also the Customer, so I need to send something like this

data: { grant_type: 'password', username: UserName, password: Password, customer: Customer }

And be able to receive it in the Web Api

//here you get the context.UserName, context.Password and context.Customer
like image 536
Victor Hugo Terceros Avatar asked Dec 14 '25 10:12

Victor Hugo Terceros


2 Answers

In the ValidateClientAuthentication you can get the additional param and add it to the context

public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            //Here we get the Custom Field sent in /Token
            string[] customer = context.Parameters.Where(x => x.Key == "customer").Select(x => x.Value).FirstOrDefault();
            if (customer.Length > 0 && customer[0].Trim().Length > 0)
            {
                context.OwinContext.Set<string>("Customer", customer[0].Trim());
            }
            // Resource owner password credentials does not provide a client ID.
            if (context.ClientId == null)
            {
                context.Validated();
            }

            return Task.FromResult<object>(null);
        }

Then use it in the where you want method GrantResourceOwnerCredentials

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //Here we use the Custom Field sent in /Token
            string customer = context.OwinContext.Get<string>("Customer");
}
like image 130
Verthosa Avatar answered Dec 15 '25 23:12

Verthosa


I found a solution

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //here you read all the params
            var data = await context.Request.ReadFormAsync();
            //here you get the param you want
            var param = data.Where(x => x.Key == "CustomParam").Select(x => x.Value).FirstOrDefault();
            string customer = "";
            if (param != null && param.Length > 0)
            {
                customer = param[0];
            }

}

What you send in the Ajax call is

data: { grant_type: 'password', username: user, password: pwd, CustomParam: 'MyParam' },

You can download a running sample in my github repository

like image 20
Victor Hugo Terceros Avatar answered Dec 15 '25 23:12

Victor Hugo Terceros