【问题标题】:How to solve this "No authenticationScheme was specified, and there was no DefaultChallengeScheme found"如何解决此“未指定身份验证方案,并且未找到 DefaultChallengeScheme”
【发布时间】:2021-02-22 03:41:24
【问题描述】:

[![消息错误]] [1]:https://i.stack.imgur.com/ciKCK.png

我的意思是,如果有人在没有登录的情况下进入授权页面,它将被重定向到登录页面。

这是我的代码(.NET Core 3.1):

我的控制器

    [Authorize]
    public IActionResult Username()
    {
        var lstUsername = _dbContext.MT_USERNAME.ToList();
        return View(lstUsername);
    }

我的 Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<GPServiceDbContext>(options =>
        {
            options.UseSqlServer(xxx);
        });
        services.AddControllersWithViews();

        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(1);   
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        app.UseStatusCodePages(context =>
        {
            var response = context.HttpContext.Response;
            if (response.StatusCode == (int)HttpStatusCode.Unauthorized ||
                response.StatusCode == (int)HttpStatusCode.Forbidden)
                response.Redirect("/Login/Login");
            return Task.CompletedTask;
        });
        app.UseAuthorization();
        app.UseSession();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Login}/{action=Login}/{id?}");
        });
    }

【问题讨论】:

标签: c# asp.net-mvc asp.net-core .net-core visual-studio-code


【解决方案1】:

因为您没有在 ConfigureServices 中配置身份验证服务。以cookie认证为例。需要指定默认的 authenticationScheme。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = DefaultAuthenticationTypes.ApplicationCookie;
        })
        .AddCookie(DefaultAuthenticationTypes.ApplicationCookie, options =>
        {
            
            options.LoginPath = "/Login";
            options.LogoutPath = "/Logout";
        });

        //...
    }

关于这个,你也可以参考这些文档。

ASP.NET Core authentication

Use cookie authentication without ASP.NET Core Identity

【讨论】:

  • 谢谢卡尼。但是如果我使用 session 这个代码仍然有效?
  • 我收到错误“当前上下文中不存在名称‘DefaultAuthenticationTypes’”,我使用“使用 Microsoft.AspNetCore.Identity;”已经。
  • @GPService, DefaultAuthenticationTypes 是身份框架的一部分,在文件顶部添加一个 using。 using Microsoft.AspNet.Identity;
【解决方案2】:

在我的情况下,身份验证选项参数只是一个简单的错误

options.DefaultAuthenticateScheme

而不是这个:

options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 2019-06-23
    • 2015-07-25
    • 1970-01-01
    • 2016-06-08
    • 2021-09-21
    • 2018-07-24
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多