У меня есть веб-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 }
И сможете получить его в веб-API
//here you get the context.UserName, context.Password and context.Customer



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


В 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);
}
Затем используйте его там, где хотите, метод GrantResourceOwnerCredentials
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
//Here we use the Custom Field sent in /Token
string customer = context.OwinContext.Get<string>("Customer");
}
Я нашел решение
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' },
Вы можете скачать текущий образец в мой репозиторий github
Спасибо. Работал на меня