mntp,艳女蒙难,集思广益
在同一个域名下有很多子系统
如:a.giant.com b.giant.com c.giant.com等
但是这些系统都是giant.com这个子域。
这样的情况就可以在不引用其它框架的情况下,直接基于cookie实现同域单点登录sso
注:用id4,oauth,其它sso框架也同样可以实现。本文不讨论。
为了简单表示。在这里登录页只输入一个用户名,然后登录
后台接收到登录名后,构建登录信息。然后登录
代码如下:
<form enctype="application/x-www-form-urlencoded" method="post"> @if (!user.identity.isauthenticated) { <div><label>用户名:<input type="text" name="username" /></label><button type="submit">登录</button></div> } else { <div><label>用户名:@user.identity.name</label><a href="/home/signout">退出</a></div> } </form>
public class homecontroller : controller { public iactionresult index() { return view(); } [httppost] public async task<iactionresult> index(string username) { var claims = new list<claim> { new claim(claimtypes.name, username) }; var claimsidentity = new claimsidentity(claims, cookieauthenticationdefaults.authenticationscheme); await httpcontext.signinasync(cookieauthenticationdefaults.authenticationscheme, new claimsprincipal(claimsidentity)); return redirecttoaction("index"); } public async task<iactionresult> signout() { await httpcontext.signoutasync(cookieauthenticationdefaults.authenticationscheme); return redirecttoaction("index"); } }
public class startup { public void configureservices(iservicecollection services) { services.addauthentication(cookieauthenticationdefaults.authenticationscheme).addcookie(); services.addmvc().setcompatibilityversion(microsoft.aspnetcore.mvc.compatibilityversion.version_2_2); } // 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(); } app.useauthentication(); app.usemvcwithdefaultroute(); } }
这样就可以实现一个子系统的简单登录,效果如下
同样创建一个com.webb.sso,写同样的代码实现登录。
但是这样只能a站点登录a系统,b站点登录b系统。两个系统相互独立
如果我们要实现auser登录a系统后,b系统也自动登录auser。
那么就可做如下改造
public void configureservices(iservicecollection services) { services.adddbcontext<ssocontext>(option => { option.usesqlserver(configuration.getconnectionstring("sso")); }); services.adddataprotection() .persistkeystodbcontext<ssocontext>() //把加密数据保存在数据库 //.persistkeystofilesystem(new directoryinfo(@"\\server\share\directory\")) //把加密信息保存大文件夹 .setapplicationname("sso"); //把所有子系统都设置为统一的应用名称 services.addauthentication(cookieauthenticationdefaults.authenticationscheme) .addcookie(cookieauthenticationdefaults.authenticationscheme,options=> { options.cookie.name = ".aspnet.sharedcookie";//设置统一的cookie名称 options.cookie.domain = ".giant.com";//设置cookie的域为根域,这样所有子域都可以发现这个cookie }); services.addmvc().setcompatibilityversion(microsoft.aspnetcore.mvc.compatibilityversion.version_2_2); }
主要是增加了services.adddataprotection配置
其中,数据加密配置保存方式现阶段asp.net core支持
1。保存到文件:persistkeystofilesystem
2。保存到数据库:persistkeystodbcontext<context>
3。保存到redis:persistkeystostackexchangeredis
4。保存到azure:persistkeystoazureblobstorage
当然也可以自己实现存储方式,实现ixmlrepository
我这里实现了保存到数据库,代码如下:
public class ssocontext : dbcontext, idataprotectionkeycontext { public ssocontext(dbcontextoptions<ssocontext> option) : base(option) { } public dbset<dataprotectionkey> dataprotectionkeys { get; set; } }
主要就是在dbcontext基础上实现接口:idataprotectionkeycontext
这里修改配置主要统一了数据加密方式与统一应用名称
这样其它子域的cookie加密数据就能识别。
再配置统一的cookie名称与写的域名为根域。
这样所有子域都能发现与识别此登录的cookie信息
这样就可以实现一个系统登录,其它子系统都登录
一个子系统退出。其它子系统也都退出的功能
源代码下载地址:https://github.com/giantliu/com.sso
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论