gpt4 book ai didi

javascript - Owin、GrantResourceOwnerCredentials 发送自定义参数

转载 作者:行者123 更新时间:2023-11-30 14:42:52 24 4
gpt4 key购买 nike

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

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

然后在你想要的地方使用它方法 GrantResourceOwnerCredentials

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
//Here we use the Custom Field sent in /Token
string customer = context.OwinContext.Get<string>("Customer");
}

关于javascript - Owin、GrantResourceOwnerCredentials 发送自定义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49394310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com