Asp.net web api OAuthAuthorizationServerProvider的\u授权无效
我正在为我的WebAPi编写完全定制的ASP.NET标识 我以以下方式重写了自己的派生OAuthAuthorizationServerProvider:Asp.net web api OAuthAuthorizationServerProvider的\u授权无效,asp.net-web-api,asp.net-identity,Asp.net Web Api,Asp.net Identity,我正在为我的WebAPi编写完全定制的ASP.NET标识 我以以下方式重写了自己的派生OAuthAuthorizationServerProvider: public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { context.Validated(); return Task.FromResult<object>(n
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
return Task.FromResult<object>(null);
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
// Check User availability ...
//ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
// if i couldn't found user in my DataBase ...
//if (user == null)
//{
//context.SetError("invalid_grant", "The user name or password is incorrect.");
// return;
//}
context.Validated();
}
}
公共覆盖任务ValidateClientAuthentication(OAuthValidateClientAuthenticationContext)
{
context.Validated();
返回Task.FromResult(空);
}
公共重写异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentials上下文)
{
//检查用户可用性。。。
//ApplicationUser user=await userManager.FindAsync(context.UserName,context.Password);
//如果我在数据库中找不到用户。。。
//if(user==null)
//{
//SetError(“无效的授权”,“用户名或密码不正确”);
//返回;
//}
context.Validated();
}
}
GrantResourceOwnerCredentials
仅为每个调用返回一个无效的\u grant
错误。我想处理它,但我不知道如何处理。ValidateClientAuthentication是您进行身份验证检查的地方,如果有不匹配的地方,您会在这里抛出错误
将代码移到那里,并在调用context.Validated()之前进行检查。只有在确保所有内容都正确验证后,才能调用Validate方法
下面是我不久前做的一个实现示例:
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
string clientId;
string clientSecret;
//first try to get the client details from the Authorization Basic header
if (!context.TryGetBasicCredentials(out clientId, out clientSecret))
{
//no details in the Authorization Header so try to find matching post values
context.TryGetFormCredentials(out clientId, out clientSecret);
}
if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(clientSecret))
{
context.SetError("client_not_authorized", "invalid client details");
return Task.FromResult<object>(null);
}
var dataLayer = new RepoManager(new DataLayerDapper()).DataLayer;
var audienceDto = dataLayer.GetAudience(clientId);
if (audienceDto == null || !clientSecret.Equals(audienceDto.Secret))
{
context.SetError("unauthorized_client", "unauthorized client");
return Task.FromResult<object>(null);
}
context.Validated();
return Task.FromResult<object>(null);
}
公共覆盖任务ValidateClientAuthentication(OAuthValidateClientAuthenticationContext)
{
字符串clientId;
字符串clientSecret;
//首先尝试从Authorization Basic标头获取客户端详细信息
if(!context.TryGetBasicCredentials(out-clientId,out-clientSecret))
{
//授权标头中没有详细信息,因此请尝试查找匹配的post值
TryGetFormCredentials(out clientId,out clientSecret);
}
if(string.IsNullOrWhiteSpace(clientId)| string.IsNullOrWhiteSpace(clientSecret))
{
SetError(“客户端未授权”,“客户端详细信息无效”);
返回Task.FromResult(空);
}
var dataLayer=newrepomanager(newdatalayerdapper()).dataLayer;
var audencedto=dataLayer.GetAudience(clientId);
if(audencedto==null | |!clientSecret.Equals(audencedto.Secret))
{
SetError(“未经授权的客户端”、“未经授权的客户端”);
返回Task.FromResult(空);
}
context.Validated();
返回Task.FromResult(空);
}
请注意检查是如何按顺序进行的,某些错误是由一些适当的错误引起的
此代码从授权头中获取客户机id和客户机机密,但您可以轻松删除所有这些内容,并用自己的检查和数据库调用替换它们
重要的是,这是你处理这类事情的地方,这是你设置错误的地方,这样你的客户就知道发生了什么
GrantResourceOwnerCredentials这是您在正确验证呼叫后得到的信息,此时您可以开始创建令牌、添加声明和创建身份验证票证。如果前一个方法未能对请求进行身份验证,则不会命中此方法
以下是一个工作示例:
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var identity = new ClaimsIdentity("JWT");
identity.AddClaim(new Claim("clientID", context.ClientId));
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{
"audience", context.ClientId
}
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
return Task.FromResult<object>(null);
}
公共覆盖任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentials上下文)
{
context.OwinContext.Response.Headers.Add(“访问控制允许来源”,新[]{“*”});
var标识=新的索赔实体(“JWT”);
identity.AddClaim(新声明(“clientID”,context.clientID));
var props=新的AuthenticationProperties(新字典
{
{
“受众”,context.ClientId
}
});
var票证=新的身份验证票证(身份、道具);
上下文。已验证(票证);
返回Task.FromResult(空);
}
现在,如果您得到一个无效的授权错误,这通常是因为您没有在初始调用中设置授权类型,或者您设置了错误的值
在我的情况下,我必须设置以下内容:
“授权类型”、“密码”