首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在.Net-Core中使用带OAuth身份验证的JWT

在.Net-Core中使用带OAuth身份验证的JWT
EN

Stack Overflow用户
提问于 2019-01-31 08:48:10
回答 1查看 357关注 0票数 0

我在.Net-Core中有一个自定义的.AddOAuth()实现。我已经使用Coinbase (基本上是add google实现的克隆,外加一些特定于coinbase的自定义选项) full source为身份验证创建了一个nuget包。我已经看过一些关于这方面的其他questions,但是它们似乎没有实现OAuth (例如,我不能传递作用域)我想使用OAuth登录,但我想给我的客户返回一个JWT。

当我尝试将JWT与AddCoinbase (它只是AddOAuth的一个派生物)一起使用时

代码语言:javascript
复制
services.AddAuthentication(JWT_BEARER_AUTH)
.AddJwtBearer(cfg =>
{
    cfg.RequireHttpsMetadata = false;
    cfg.SaveToken = true;

    cfg.TokenValidationParameters = new TokenValidationParameters()
    {
        ValidIssuer = Configuration["Tokens:Issuer"],
        ValidAudience = Configuration["Tokens:Issuer"],
        //TODO: get key from secret section
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
     };
 })
 .AddCoinbase(options => {
     options.AccessAllAccounts = true;
     options.SendLimitAmount = 1;
     options.SendLimitCurrency = "USD";
     options.SendLimitPeriod = SendLimitPeriod.day;
     options.ClientId = Configuration["Coinbase:ClientId"];
     options.ClientSecret = Configuration["Coinbase:ClientSecret"];
     COINBASE_SCOPES.ForEach(scope => options.Scope.Add(scope));
     options.SaveTokens = true;
     options.ClaimActions.MapJsonKey("urn:coinbase:avatar", "avatar_url");
 });

登录到coinbase后,外部回调会重定向我

代码语言:javascript
复制
[HttpGet("ExternalLoginCallback")]
[AllowAnonymous]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
{
    if (remoteError != null)
    {
        //TODO: Handle remote error failure
        throw new Exception($"Error from external provider: {remoteError}");            
    }
    var info = await _signInManager.GetExternalLoginInfoAsync();
    if (info == null)
    {
        //TODO: Handle null external login info
        throw new Exception("Error: could not find user info");
    }

    // Sign in the user with this external login provider if the user already has a login.
    var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);1

    var user = await (result.Succeeded ?
            _userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey)
        : this.CreateIdentityUser(info));

     await _signInManager.UpdateExternalAuthenticationTokensAsync(info);
    _logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider);

    return Redirect(returnUrl);
}

在重定向之后,我从未收到过JSON Web令牌,我总是收到Cookie。如何在向客户提供OAuth身份验证的同时利用JWT身份验证?

EN

回答 1

Stack Overflow用户

发布于 2019-01-31 10:31:31

OAuth不是Json Web Token解决方案。OIDC2.0提供授权和可选标识( OAuth )。

当您通过OAuth 2.0端点进行授权时,您将收到一个访问令牌和一个可选的ID令牌。ID令牌是一个带符号的JWT。访问令牌是一个不透明的对象,它是一些供应商实现的签名JWT,但不是所有的(Google是不透明的)。

授权后,您将收到一个或两个令牌(访问权限和ID)。您可以将它们包装在您自己的JWT中,对其进行签名,然后以任何您想要的方式使用组合的JWT。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54451807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档