最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Owin, GrantResourceOwnerCredentials send custom parameters - Stack Overflow

programmeradmin3浏览0评论

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

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
Share Improve this question edited Mar 21, 2018 at 16:23 Victor Hugo Terceros asked Mar 20, 2018 at 21:21 Victor Hugo TercerosVictor Hugo Terceros 3,1693 gold badges20 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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

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

发布评论

评论列表(0)

  1. 暂无评论