我是.net中的Oauth和Owin的初学者。我试图理解这些方法dalitateclientauthentication方法和GrantresourceownerCredentials方法。我了解到,GrantresourceownerCredentials方法可用于验证凭据并产生令牌。然后,方法是什么目的dalotateclientauthentication()。请指导我。非常感谢。
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
return Task.Factory.StartNew(() =>
{
var userName = context.UserName;
var password = context.Password;
var userService = new UserService(); // our created one
var user = userService.ValidateUser(userName, password);
if (user != null)
{
var claims = new List<Claim>()
{
new Claim(ClaimTypes.Sid, Convert.ToString(user.Id)),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Email, user.Email)
};
ClaimsIdentity oAuthIdentity = new ClaimsIdentity(claims,Startup.OAuthOptions.AuthenticationType);
var properties = CreateProperties(user.Name);
var ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
}
else
{
context.SetError("invalid_grant", "The user name or password is incorrect");
}
});
}
#endregion
#region[ValidateClientAuthentication]
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
if (context.ClientId == null)
context.Validated();
return Task.FromResult<object>(null);
}
#endregion
这与客户凭证流与资源所有者密码凭据流相关的OAuth 2.0 Spec
请记住,客户和资源所有者是Oauth下的不同实体。客户代表资源所有者提出请求。
实际上,当您期望接受实际的用户名和密码并发出访问令牌时,您需要使用GrantresourceownerCredentials。
应使用valiTateClientAuthentication来确保客户端所说的。如果已将客户端注册到授权服务器并需要验证其仍然有效。
,您可能会这样做。我所看到的大多数代码示例都像您在示例中所做的那样calling context.validated()。我能够找到一篇博客文章,其中包含一个代码样本,可以深入研究。在此处查看:http://bitoftech.net/2014/10/10/27/json-web-token-asp-net-web-api-web-api-2-jwt-owin-authorization-server/perver/perver/perver/phive/p>