Asp.Net Core基於Cookie實現同域單點登錄(SSO)

在同一個域名下有很多子系統

如: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章