【问题标题】:Auth0 Authentication for .Net Core Web API.Net Core Web API 的 Auth0 身份验证
【发布时间】:2017-04-27 21:00:51
【问题描述】:

我在 .net core 1.1 中创建了一个 Web API。我已经使用 Auth0 通过社交登录对其进行身份验证。选择的客户端类型是“API 的 BETA”。 我在 .net core 1.1 中创建了另一个 Web 应用程序,它使用另一个 Auth0 [常规 Web 应用程序] 来验证社交登录。

这是否可以使用 Web 应用程序创建的访问令牌作为 Authorization 标头传递并获得对 Web api 方法的访问权限?

谢谢, 森迪尔

【问题讨论】:

    标签: c# authentication asp.net-core jwt auth0


    【解决方案1】:

    我在 Auth0 上写了一篇关于 API authentication with JWT tokens on ASP.NET Core 以及如何使用 Autorest 创建客户端的帖子。

    该帖子有一个public GitHub repo 提供整个场景(Web 应用程序 + API)。

    基本上,您传递 JWT 令牌并在 API 上验证它,并在您的 Startup.cs 上使用它:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    
    /*Enabling swagger file*/
    
    app.UseSwagger();
    
    /*Enabling Swagger ui, consider doing it on Development env only*/
    
    app.UseSwaggerUi();
    
    /*It's important that this is AFTER app.UseSwagger or the swagger.json file would be innaccesible*/
    
    var options = new JwtBearerOptions
    
    {
    
        Audience = Configuration["auth0:clientId"],
    
        Authority = $"https://{Configuration["auth0:domain"]}/"
    
    };
    
    app.UseJwtBearerAuthentication(options);
    
    /*Normal MVC mappings*/
    
    app.UseMvc();
    

    }

    【讨论】:

    • 谢谢马蒂亚斯。这有帮助
    • 好吧,我很困惑。似乎每个人都将 JwtBearerOptions 对象传递给不采用该对象的方法 - app.UseJwtBearerAuthentication - 它采用 JwtBearerAuthenticationOptions 代替。有没有关于如何做到这一点的工作示例? (Auth0 的网站也说要使用上面的代码)如果这很重要,我在 .Net 4.7 上
    • @traderhutGames - 我认为这是针对 .net 核心的,它在 .net 4.7 中的工作方式有所不同
    猜你喜欢
    • 2020-10-15
    • 2017-09-05
    • 2020-09-05
    • 1970-01-01
    • 2019-10-22
    • 2016-12-22
    • 2018-08-09
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多