OWIN 的 oAuth 中的 ValidateClientAuthentication 方法和 GrantResourceOwnerCredentials 方法有什么区别?
what is the difference between ValidateClientAuthentication method and GrantResourceOwnerCredentials method in oAuth of OWIN?
我是 .NET 中的 oauth 和 owin 初学者。我试图了解这些方法 ValidateClientAuthentication 方法和 GrantResourceOwnerCredentials 方法。我知道 GrantResourceOwnerCredentials 方法可用于验证凭据和生成令牌。那么,方法 ValidateClientAuthentication() 的目的是什么。请就此指导我。非常感谢。
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 规范
中的Client Credentials Flow vs. Resource Owner Password Credentials Flow相关
请记住,客户和资源所有者在 OAuth 下是不同的实体。客户代表资源所有者提出请求。
实际上,当您希望接受实际的用户名和密码并颁发访问令牌时,您会希望使用 GrantResourceOwnerCredentials。
ValidateClientAuthentication 应该用于确保客户端与其所说的一样。如果已将客户端注册到授权服务器并需要验证它是否仍然有效,您可能会这样做。
我见过的大多数代码示例只是调用 context.Validated(),就像您在示例中所做的那样。我找到了一个博客 post,其中包含更深入的代码示例。在这里查看:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/
我是 .NET 中的 oauth 和 owin 初学者。我试图了解这些方法 ValidateClientAuthentication 方法和 GrantResourceOwnerCredentials 方法。我知道 GrantResourceOwnerCredentials 方法可用于验证凭据和生成令牌。那么,方法 ValidateClientAuthentication() 的目的是什么。请就此指导我。非常感谢。
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 规范
中的Client Credentials Flow vs. Resource Owner Password Credentials Flow相关请记住,客户和资源所有者在 OAuth 下是不同的实体。客户代表资源所有者提出请求。
实际上,当您希望接受实际的用户名和密码并颁发访问令牌时,您会希望使用 GrantResourceOwnerCredentials。
ValidateClientAuthentication 应该用于确保客户端与其所说的一样。如果已将客户端注册到授权服务器并需要验证它是否仍然有效,您可能会这样做。
我见过的大多数代码示例只是调用 context.Validated(),就像您在示例中所做的那样。我找到了一个博客 post,其中包含更深入的代码示例。在这里查看:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/