没有Identity ASP.NET Core v2的Cookie中间件

时间:2017-06-14 15:12:21

标签: c# asp.net .net-core asp.net-core-2.0

我试图在不使用身份的情况下进行身份验证。我发现了一些文章描述了如何在其他版本中执行此操作,但对于ASP.NET Core 2没有任何内容。

以下是我拼凑在一起的内容。但是当它到达SignInAsync时,会抛出异常InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
        {
            o.LoginPath = new PathString("/Account/Login/");
            o.AccessDeniedPath = new PathString("/Account/Forbidden/");

        });
        services.AddAuthentication();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

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

        app.UseAuthentication();
    }

    public async Task<IActionResult> Login()
    {

        var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, "joe nobody")
            };
        var identity = new ClaimsIdentity(claims, "MyCookieMiddlewareInstance");
        var principal = new ClaimsPrincipal(identity);

        //blows up on the following statement:
        //InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
        await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal); 

        return View();
    }

有一个针对asp.net core v1.x(https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie)的Microsoft文档,但是在v2中对IApplicationBuilder.UseCookieAuthentication()进行了折旧,并且找不到任何解决方案。

1 个答案:

答案 0 :(得分:4)

使用Auth 2.0(https://github.com/aspnet/Announcements/issues/232

看起来有一些重大变化

设置正确但我需要做两件事:

  1. 使用HttpContext.SignInAsync()using Microsoft.AspNetCore.Authentication)代替HttpContext.Authentication.SignInAsync()
  2. 使用"AuthenticationTypes.Federation"作为身份验证类型 (注意:其他值似乎不起作用,空白将导致用户名设置并且IsAuthenticated为false) var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");
  3. 以下是更正后的代码

    在Startup.cs

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
            {
                o.LoginPath = new PathString("/Account/Login/");
                o.AccessDeniedPath = new PathString("/Account/Forbidden/");
            });
            services.AddAuthentication();
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
    
            app.UseStaticFiles();
    
            app.UseAuthentication();
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    

    在控制器

        using Microsoft.AspNetCore.Authentication;
        //...
        public async Task<IActionResult> Login()
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, "joe nobody")
            };
            var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");
            var principal = new ClaimsPrincipal(identity);
            await HttpContext.SignInAsync("MyCookieMiddlewareInstance", principal);
    
            return View();
        }