如何配置AspNetCore标识和Auth0

时间:2017-02-07 03:26:57

标签: c# asp.net asp.net-identity auth0

我使用ASP.NET核心模板1.1(Web应用程序)在VS 2017中创建了一个示例项目,将身份验证更改为单个用户帐户。

从那里我按照

的说明进行操作

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/

https://auth0.com/docs/quickstart/webapp/aspnet-core/01-login

Startup.cs本质上是

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddAuthentication(
            options =>
            {
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            });

        services.AddMvc(options=>
        {
            options.SslPort = 44321;
            options.Filters.Add(new RequireHttpsAttribute());
        });

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();

        services.AddOptions();

        services.Configure<Auth0Settings>(Configuration.GetSection("Auth0"));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions<Auth0Settings> auth0Settings)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseIdentity();

        // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AutomaticAuthenticate = true,
            AutomaticChallenge = true
        });

        var options = new OpenIdConnectOptions("Auth0")
        {
            Authority = $"https://{auth0Settings.Value.Domain}",

            ClientId = auth0Settings.Value.ClientId,
            ClientSecret = auth0Settings.Value.ClientSecret,

            AutomaticAuthenticate = false,
            AutomaticChallenge = false,
            SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme,

            ResponseType = "code",

            CallbackPath = new PathString("/signin-auth0"),

            // Configure the Claims Issuer to be Auth0
            ClaimsIssuer = "Auth0",

        };
        options.Scope.Clear();
        options.Scope.Add("openid");
        app.UseOpenIdConnectAuthentication(options);


        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

我注册用户,导航以管理您的帐户,然后点击外部登录旁边的管理链接。

我单击Auth0按钮并成功登录。我被重定向到/ Manage / LinkLoginCallback,其中以下方法返回null。

private Task<ApplicationUser> GetCurrentUserAsync()
{
    return _userManager.GetUserAsync(HttpContext.User);
}

导致出现以下内容

  

错误。处理您的请求时发生错误。发展   模式交换到开发环境将显示更详细   有关发生的错误的信息。

     

不应在部署中启用开发环境   应用程序,因为它可能导致敏感信息   向最终用户显示例外情况。对于本地调试,   可以通过设置启用开发环境   ASPNETCORE_ENVIRONMENT环境变量到开发,和   重新启动应用程序。

我不确定在配置Auth0和身份方面我做错了什么。另外,如果你知道如何在launchSettings.json确实将ASPNETCORE_ENVIRONMENT作为开发时解决上面的错误,那么也会非常感激。

0 个答案:

没有答案
相关问题