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 =>
{
// 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.youkuaiyun.com/CameronAnderson/article/details/117852362
本文介绍了如何在.NET Core应用中集成CAS(Central Authentication Service)进行统一身份认证。首先,从git源码地址获取并安装NuGet包。接着,在启动方法中配置CAS服务,并在appsettings配置文件中添加CasBaseUrl设置。然后,在需要授权的控制器上使用[Authorize]特性。当遇到登录失败问题时,需要更新Cookie策略以兼容最新浏览器,将MinimumSameSitePolicy设置为Lax。详细原因和解决方案参考链接。
484

被折叠的 条评论
为什么被折叠?



