如何正确使用dotnet core mvc中的CookieAuthentication?

时间:2016-12-02 07:49:55

标签: c# cookies asp.net-core middleware

我的配置:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {          
        app.UseCookieAuthentication( new CookieAuthenticationOptions
        {
            AuthenticationScheme = "MyScheme",
            LoginPath = new Microsoft.AspNetCore.Http.PathString( "/Account/Login" ),
            AccessDeniedPath = new Microsoft.AspNetCore.Http.PathString( "/Account/Forbidden" ),
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
        } );

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

我的ConfigureServices:

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);

        services.AddMvc();
        services.AddAuthentication();
        services.AddAuthorization();
    }

问题是,当我尝试使用Authorize属性进行任何操作时,服务器只回复httpcode 401,但我想要的是一个httpcode 302。

0 个答案:

没有答案