我们的应用程序需要通过手机号码或谷歌登录。我们计划使用Twitter数字进行移动号码认证。
据我所知,登记和认证的流程如下:
现在,我正在寻找实现步骤3-4的选项。
目前,我所做的是修改令牌端点代码,接受用户名作为电话号码或电子邮件地址和密码发送为Google/Twitter数字ID令牌。现在,由于auth服务器需要知道发送的密码实际上是一个需要第三方服务验证的令牌,所以我在TokenHint中发送服务名“位”或“Google”。
这是很好的工作,但我想知道什么是最清洁的方式来支持我正在努力实现的目标。
发布于 2016-12-19 14:00:04
这是很好的工作,但我想知道什么是最清洁的方式来支持我正在努力实现的目标。
我个人会选择一种定制的赠与类型:
[HttpPost("~/connect/token")]
[Produces("application/json")]
public IActionResult Exchange(OpenIdConnectRequest request)
{
if (request.GrantType == "urn:ietf:params:oauth:grant-type:google_identity_token")
{
// Reject the request if the "assertion" parameter is missing.
if (string.IsNullOrEmpty(request.Assertion))
{
return BadRequest(new OpenIdConnectResponse
{
Error = OpenIdConnectConstants.Errors.InvalidRequest,
ErrorDescription = "The mandatory 'assertion' parameter was missing."
});
}
// Create a new ClaimsIdentity containing the claims that
// will be used to create an id_token and/or an access token.
var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
// Manually validate the identity token issued by Google,
// including the issuer, the signature and the audience.
// Then, copy the claims you need to the "identity" instance.
// Create a new authentication ticket holding the user identity.
var ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
OpenIdConnectServerDefaults.AuthenticationScheme);
ticket.SetScopes(
OpenIdConnectConstants.Scopes.OpenId,
OpenIdConnectConstants.Scopes.OfflineAccess);
return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
}
return BadRequest(new OpenIdConnectResponse
{
Error = OpenIdConnectConstants.Errors.UnsupportedGrantType,
ErrorDescription = "The specified grant type is not supported."
});
}请注意,您还必须在OpenIddict选项中启用它:
// Register the OpenIddict services.
services.AddOpenIddict()
// Register the Entity Framework stores.
.AddEntityFrameworkCoreStores<ApplicationDbContext>()
// Register the ASP.NET Core MVC binder used by OpenIddict.
// Note: if you don't call this method, you won't be able to
// bind OpenIdConnectRequest or OpenIdConnectResponse parameters.
.AddMvcBinders()
// Enable the token endpoint.
.EnableTokenEndpoint("/connect/token")
// Enable the refresh token flow and a custom grant type.
.AllowRefreshTokenFlow()
.AllowCustomFlow("urn:ietf:params:oauth:grant-type:google_identity_token")
// During development, you can disable the HTTPS requirement.
.DisableHttpsRequirement();在发送令牌请求时,请确保使用正确的grant_type并将id_token作为assertion参数发送,它应该可以工作。
下面是一个使用Facebook访问令牌的示例:

在实现令牌验证例程时,要非常小心,因为这一步骤特别容易出错。验证所有内容非常重要,包括受众(否则,您的服务器将容易受到混乱的代理攻击。)。
https://stackoverflow.com/questions/41223694
复制相似问题