【问题标题】:Does ASP.Net MVC6 support OAuth 2 bearer tokens?ASP.Net MVC6 是否支持 OAuth 2 不记名令牌?
【发布时间】:2015-09-09 12:41:45
【问题描述】:

我正在使用 ASP.Net MVC6 开发应用程序,我想使用不记名令牌实现 OAuth 2 身份验证。我找不到任何关于这是否可能的可靠信息。谁能指出我正确的方向?

【问题讨论】:

  • 看到这个post,好像他们没有包含它。

标签: asp.net oauth-2.0 asp.net-core-mvc


【解决方案1】:

TL;DR:微软为 ASP.NET Core 开发的官方包只支持 OAuth2 不记名令牌验证。

这意味着……

  1. ...您将能够使用由外部身份提供者(如 Azure Active Directory)通过 Microsoft.AspNetCore.Authentication.JwtBearer 包颁发的不记名令牌来验证您的用户:

    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthentication = true,
        Audience = "http://localhost:50000/",
    
        // Authority is only useful if your JWT tokens
        // are issued by an OpenID Connect server.
        Authority = "[OpenID Connect provider address]",
    
        // If you don't use an OpenID Connect server, you have to manually update the
        // token validation parameters with the issuer's signing key.
        TokenValidationParameters = new TokenValidationParameters
        {
            IssuerSigningKey = new X509SecurityKey(certificate)
        }
    });
    

也就是说,现在仅支持 JWT 令牌 OTB:Katana 3 附带的 OAuth2 不记名中间件用于原生支持由 OAuth2 授权服务器生成的不透明令牌,但此支持已被删除。

  1. ...您将无法再生产自己的令牌。 OAuth2 授权服务器已被移除,不会移植到 ASP.NET Core:OAuth Authorization Service in ASP.NET Core。

幸运的是,存在替代方案。我个人正在开发一个基于 Katana 附带的 OAuth2 服务器的 OpenID Connect 服务器中间件,它提供了相同的低级体验:https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server

有关更多信息,您可以查看此 SO 答案:Configure the authorization server endpoint

【讨论】:

  • 我还是有点困惑;我们可以生成自己的 JWT 吗?我目前正在设计一个 Web API,但我不知道我要做什么,因为我希望能够提供身份验证令牌。我不希望用户通过 GitHub 等第三方登录。
  • 使用 ASP.NET 5 原生提供的安全中间件?不,使用第三方服务器?是的。
  • 在开发 REST API 时确实很不方便。我可能最终会设计自己的系统。
  • 你应该看看这个样本:github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/…。它使用了我在回答中提到的项目(OpenIdConnectServerMiddleware),基本上相当于 ASP.NET 5 中的OAuthAuthorizationServerMiddleware。
  • @Pinpoint-知道这有点旧,但我在任何地方都找不到任何解决方案。在您的示例中,“证书”在哪里定义?我有一个我曾经使用过的字符串 base64 编码密钥,现在我需要一个证书,我迷失了如何将我曾经使用的字符串密钥转换为 509 证书
猜你喜欢
  • 2021-06-02
  • 2020-03-27
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 2013-12-01
  • 2014-06-05
  • 1970-01-01
相关资源
最近更新 更多