git源码地址:https://github.com/IUCrimson/AspNet.Security.CAS
安装 NuGet 包
PM> Install-Package AspNetCore.Security.CAS
打开 Startup.cs
在您的启动ConfigureServices方法中:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/login");
})
.AddCAS(options =>
{
options.CasServerUrlBase = Configuration["CasBaseUrl"]; // Set in `appsettings.json` file.
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
5.
[AllowAnonymous]
[Route("login")]
public async Task Login(string returnUrl)
{
var props = new AuthenticationProperties { RedirectUri = returnUrl };
await HttpContext.ChallengeAsync("CAS", props);
}
6、在appsettings配置文件中添加CasBaseUrl节点,节点的内容为CAS服务端的地址
例:"CasBaseUrl": "https://localhost:8080/cas"
7、添加完成之后在应用启动后第一个访问的控制器的Action上添加 [Authorize]特性
这时如果你没有通过CAS服务端的身份验证的话就会跳转到CAS登录页面,
8、上述步骤做完以后谷歌等最新浏览器会出现登录失败,需要在ConfigureServices修改cookie策略!!!!!!!!!!
services.Configure<CookiePolicyOptions>(options =>
{undefined
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
//修改浏览器cookie策略为lax
options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.Lax;
});
具体原因请跳转
https://blog.csdn.net/CameronAnderson/article/details/117852362
推荐阅读