C# 如何配置AspNetCore标识和身份验证0

C# 如何配置AspNetCore标识和身份验证0,c#,asp.net,asp.net-identity,auth0,C#,Asp.net,Asp.net Identity,Auth0,我在VS 2017中创建了一个示例项目,使用ASP.NET核心模板1.1(Web应用程序)将身份验证更改为个人用户帐户 从那以后,我按照办公室的指示去做 及 Startup.cs本质上是 public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext<ApplicationDbCont

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

从那以后,我按照办公室的指示去做

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?}");
        });
    }
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddAuthentication(
选项=>
{
options.signnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
});
services.AddMvc(选项=>
{
options.SslPort=44321;
options.Filters.Add(新的requireHttpAttribute());
});
//添加应用程序服务。
services.AddTransient();
services.AddTransient();
services.AddOptions();
Configure(Configuration.GetSection(“Auth0”);
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、ILoggerFactory loggerFactory、IOptions auth0Settings)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseIdentity();
//在下面添加外部身份验证中间件。要配置它们,请参阅http://go.microsoft.com/fwlink/?LinkID=532715
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
自动验证=真,
自动挑战=真
});
var options=new OpenIdConnectOptions(“Auth0”)
{
Authority=$“https://{auth0Settings.Value.Domain}”,
ClientId=auth0Settings.Value.ClientId,
ClientSecret=auth0Settings.Value.ClientSecret,
自动验证=错误,
自动挑战=错误,
Signnscheme=CookieAuthenticationDefaults.AuthenticationScheme,
ResponseType=“code”,
回调路径=新路径字符串(“/signin-auth0”),
//将索赔颁发者配置为Auth0
ClaimsIssuer=“Auth0”,
};
options.Scope.Clear();
options.Scope.Add(“openid”);
app.UseOpenIdConnectAuthentication(选项);
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
我注册一个用户,导航到管理您的帐户,然后单击外部登录旁边的“管理”链接

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

private Task<ApplicationUser> GetCurrentUserAsync()
{
    return _userManager.GetUserAsync(HttpContext.User);
}
私有任务GetCurrentUserAsync() { 返回_userManager.GetUserAsync(HttpContext.User); } 导致出现以下内容:

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

不应在部署中启用开发环境 应用程序,因为它可能会从 向最终用户显示的异常。对于本地调试, 可以通过设置 ASPNETCORE_环境变量用于开发,以及 重新启动应用程序


我不确定在配置Auth0和Identity方面我做错了什么。此外,如果您知道如何在launchSettings.json将ASPNETCORE_环境作为开发时解决上述错误,那将非常感谢。

我在注释“app.UseCookieAuthentication();”时遇到了此警告。只是暗中试探一下,也许您可以尝试在不使用CookieAuthenticationOptions参数的情况下使用CookieAuthentication?