Asp.net core 2.x 踩坑记录(一)修改cookie中间件的配置不生效

一、问题


  背景:asp.net core 2.x

  新建项目:选择的是 使用个人身份验证

  默认 未授权跳转地址 loginpath为 '''Identity/Account/Login',我想改成自己的区域内的地址 '''Admin/Account/Login'

二、解决方案


  第一次尝试:

  在网上查看说是自定义的 AuthenticationScheme名字要和 AddCookie要一致 ,就是AddAuthentication和AddCookie的第一个参数,要一样,表示不是很懂,然后这么写:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
             {
                 //options.AccessDeniedPath = "/Admin/Account/login";
                 //options.Cookie.Name = "MyCookieAuthenticationScheme";
                 //options.Cookie.HttpOnly = false;
                 //options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
                 options.LoginPath = new PathString("/admin/account/login");
                 options.LogoutPath = new PathString("/admin/account/logout");
                 // ReturnUrlParameter requires 
                 //using Microsoft.AspNetCore.Authentication.Cookies;
                 options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                 //options.SlidingExpiration = true;
             });

  结果:不好使- -  在未授权时,仍然是 跳转到 'Identity/Account/Login'。

  第二次尝试:将默认是AuthenticationScheme改成自定义的 Name字符串 “MyScheme”

services.AddAuthentication("MyScheme").AddCookie("MyScheme", options =>
             {
                 //options.AccessDeniedPath = "/Admin/Account/login";
                 //options.Cookie.Name = "MyCookieAuthenticationScheme";
                 //options.Cookie.HttpOnly = false;
                 //options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
                 options.LoginPath = new PathString("/admin/account/login");
                 options.LogoutPath = new PathString("/admin/account/logout");
                 // ReturnUrlParameter requires 
                 //using Microsoft.AspNetCore.Authentication.Cookies;
                 options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                 //options.SlidingExpiration = true;
             });

  结果:不好使仍然-  - 我操,表示core真的很坑,- - 官方文档 也没有说明白这方面的问题- - 就介绍了一些基本操作- - 这些最重要的却避而不谈。。。。设置的自定义属性都不生效,你还介绍怎么设置- -  难道不应该先说明生效的前提嘛,这里一万头羊驼- - 

  第三次尝试:这里是参考地址:https://www.cnblogs.com/piscesLoveCc/p/7366418.html 这里感谢这位仁兄- - 

services.AddAuthentication(options =>
            {
                options.DefaultChallengeScheme = "MyScheme";
                options.DefaultSignInScheme = "MyScheme";
                options.DefaultAuthenticateScheme = "MyScheme";
            }).AddCookie("MyScheme", options =>
             {
                 //options.AccessDeniedPath = "/Admin/Account/login";
                 //options.Cookie.Name = "MyCookieAuthenticationScheme";
                 //options.Cookie.HttpOnly = false;
                 //options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
                 options.LoginPath = new PathString("/admin/account/login");
                 options.LogoutPath = new PathString("/admin/account/logout");
                 // ReturnUrlParameter requires 
                 //using Microsoft.AspNetCore.Authentication.Cookies;
                 options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                 //options.SlidingExpiration = true;
             });

  结果:终于是成功了!槽

原文地址:https://www.cnblogs.com/4job/p/11192881.html