Javascript Owin,GrantResourceOwnerCredentials发送自定义参数

Javascript Owin,GrantResourceOwnerCredentials发送自定义参数,javascript,c#,asp.net-mvc,asp.net-web-api,owin,Javascript,C#,Asp.net Mvc,Asp.net Web Api,Owin,我有一个Web Api,在其中我使用Owin令牌身份验证,正如您所知,默认情况下您有此身份验证方法 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { //here you get the context.UserName and context.Password //

我有一个Web Api,在其中我使用Owin令牌身份验证,正如您所知,默认情况下您有此身份验证方法

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

$.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);
            }
        });
这很完美,但问题是我有一个多客户数据库,而且我还必须发送客户,所以我需要发送类似的内容

data: { grant_type: 'password', username: UserName, password: Password, customer: Customer }
并且能够在Web Api中接收它

//here you get the context.UserName, context.Password and context.Customer
在ValidateClientAuthentication中,可以获取附加参数并将其添加到上下文中

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);
        }
公共覆盖任务ValidateClientAuthentication(OAuthValidateClientAuthenticationContext)
{
//这里我们得到了发送到/Token的自定义字段
字符串[]customer=context.Parameters.Where(x=>x.Key==“customer”)。选择(x=>x.Value)。FirstOrDefault();
如果(customer.Length>0&&customer[0].Trim().Length>0)
{
context.OwinContext.Set(“客户”,客户[0].Trim());
}
//资源所有者密码凭据不提供客户端ID。
if(context.ClientId==null)
{
context.Validated();
}
返回Task.FromResult(空);
}
然后在您想要的方法GrantResourceOwnerCredentials中使用它

public override异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext)
{
//在这里,我们使用传入/令牌的自定义字段
字符串customer=context.OwinContext.Get(“客户”);
}
我找到了一个解决方案

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];
            }

}
在Ajax调用中发送的是

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

您可以在

中下载正在运行的示例,谢谢。为我工作
data: { grant_type: 'password', username: user, password: pwd, CustomParam: 'MyParam' },