国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

深入淺析C#中單點登錄的原理和使用

2019-10-29 21:08:58
字體:
來源:轉載
供稿:網友

什么是單點登錄

我想肯定有一部分人“望文生義”的認為單點登錄就是一個用戶只能在一處登錄,其實這是錯誤的理解(我記得我第一次也是這么理解的)。

單點登錄指的是多個子系統只需要登錄一個,其他系統不需要登錄了(一個瀏覽器內)。一個子系統退出,其他子系統也全部是退出狀態。

如果你還是不明白,我們舉個實際的例子把。比如VEVB武林網首頁:http://www.survivalescaperooms.com ,和VEVB武林網的文章頻道http://meiwen.vevb.com。這就是兩個系統(不同的域名)。如果你登錄其中一個,另一個也是登錄狀態。如果你退出一個,另一個也是退出狀態了。

那么這是怎么實現的呢?這就是我們今天要分析的問題了。

單點登錄(SSO)原理

首先我們需要一個認證中心(Service),和兩個子系統(Client)。

當瀏覽器第一次訪問Client1時,處于未登錄狀態 -> 302到認證中心(Service) -> 在Service的登錄頁面登錄(寫入Cookie記錄登錄信息) -> 302到Client1(寫入Cookie記錄登錄信息)第二次訪問Client1 -> 讀取Client1中Cookie登錄信息 -> Client1為登錄狀態

第一次訪問Client2 -> 讀取Client2中Cookie中的登錄信息 -> Client2為未登錄狀態 -> 302到在Service(讀取Service中的Cookie為登錄狀態) -> 302到Client2(寫入Cookie記錄登錄信息)

我們發現在訪問Client2的時候,中間時間經過了幾次302重定向,并沒有輸入用戶名密碼去登錄。用戶完全感覺不到,直接就是登錄狀態了。

圖解:


c#,單點登錄

手擼一個SSO

環境:.NET Framework 4.5.2

Service:

/// <summary>/// 登錄/// </summary>/// <param name="name"></param>/// <param name="passWord"></param>/// <param name="backUrl"></param>/// <returns></returns>[HttpPost]public string Login(string name, string passWord, string backUrl){ if (true)//TODO:驗證用戶名密碼登錄 {  //用Session標識會話是登錄狀態  Session["user"] = "XX已經登錄";  //在認證中心 保存客戶端Client的登錄認證碼  TokenIds.Add(Session.SessionID, Guid.NewGuid()); } else//驗證失敗重新登錄 {  return "/Home/Login"; } return backUrl + "?tokenId=" + TokenIds[Session.SessionID];//生成一個tokenId 發放到客戶端}

Client:

public static List<string> Tokens = new List<string>();public async Task<ActionResult> Index(){ var tokenId = Request.QueryString["tokenId"]; //如果tokenId不為空,則是由Service302過來的。 if (tokenId != null) {  using (HttpClient http = new HttpClient())  {   //驗證Tokend是否有效   var isValid = await http.GetStringAsync("http://localhost:8018/Home/TokenIdIsValid?tokenId=" + tokenId);   if (bool.Parse(isValid.ToString()))   {    if (!Tokens.Contains(tokenId))    {     //記錄登錄過的Client (主要是為了可以統一登出)     Tokens.Add(tokenId);    }    Session["token"] = tokenId;   }  } } //判斷是否是登錄狀態 if (Session["token"] == null || !Tokens.Contains(Session["token"].ToString())) {  return Redirect("http://localhost:8018/Home/Verification?backUrl=http://localhost:26756/Home"); } else {  if (Session["token"] != null)   Session["token"] = null; } return View();}

效果圖:


c#,單點登錄

當然,這只是用較少的代碼擼了一個較簡單的SSO。僅用來理解,勿用于實際應用。

IdentityServer4實現SSO

環境:.NET Core 2.0

上面我們手擼了一個SSO,接下來我們看看.NET里的IdentityServer4怎么來使用SSO。

首先建一個IdentityServer4_SSO_Service(MVC項目),再建兩個IdentityServer4_SSO_Client(MVC項目)
在Service項目中用nuget導入IdentityServer4 2.0.2、IdentityServer4.AspNetIdentity 2.0.0、IdentityServer4.EntityFramework 2.0.0
在Client項目中用nuget導入IdentityModel 2.14.0

然后分別設置Service和Client項目啟動端口為 5001(Service)、5002(Client1)、5003(Client2)


c#,單點登錄
在Service中新建一個類Config:

public class Config{   public static IEnumerable<IdentityResource> GetIdentityResources()  {   return new List<IdentityResource>   {    new IdentityResources.OpenId(),    new IdentityResources.Profile(),   };  } public static IEnumerable<ApiResource> GetApiResources() {  return new List<ApiResource>  {   new ApiResource("api1", "My API")  }; } // 可以訪問的客戶端 public static IEnumerable<Client> GetClients()  {      return new List<Client>   {        // OpenID Connect hybrid flow and client credentials client (MVC)    //Client1    new Client    {     ClientId = "mvc1",     ClientName = "MVC Client1",     AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,     RequireConsent = true,     ClientSecrets =     {      new Secret("secret".Sha256())     },     RedirectUris = { "http://localhost:5002/signin-oidc" }, //注意端口5002 是我們修改的Client的端口     PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },     AllowedScopes =     {      IdentityServerConstants.StandardScopes.OpenId,      IdentityServerConstants.StandardScopes.Profile,      "api1"     },     AllowOfflineAccess = true    },     //Client2    new Client    {     ClientId = "mvc2",     ClientName = "MVC Client2",     AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,     RequireConsent = true,     ClientSecrets =     {      new Secret("secret".Sha256())     },     RedirectUris = { "http://localhost:5003/signin-oidc" },     PostLogoutRedirectUris = { "http://localhost:5003/signout-callback-oidc" },     AllowedScopes =     {      IdentityServerConstants.StandardScopes.OpenId,      IdentityServerConstants.StandardScopes.Profile,      "api1"     },     AllowOfflineAccess = true    }   };  }}

新增一個ApplicationDbContext類繼承于IdentityDbContext:

public class ApplicationDbContext : IdentityDbContext<IdentityUser>{ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  : base(options) { }  protected override void OnModelCreating(ModelBuilder builder) {  base.OnModelCreating(builder); }}

在文件appsettings.json中配置數據庫連接字符串:

"ConnectionStrings": { "DefaultConnection": "Server=(local);Database=IdentityServer4_Demo;Trusted_Connection=True;MultipleActiveResultSets=true" }

在文件Startup.cs的ConfigureServices方法中增加:

public void ConfigureServices(IServiceCollection services){ services.AddDbContext<ApplicationDbContext>(options =>  options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); //數據庫連接字符串 services.AddIdentity<IdentityUser, IdentityRole>()  .AddEntityFrameworkStores<ApplicationDbContext>()  .AddDefaultTokenProviders(); services.AddMvc(); string connectionString = Configuration.GetConnectionString("DefaultConnection"); var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; services.AddIdentityServer()  .AddDeveloperSigningCredential()  .AddAspNetIdentity<IdentityUser>()   .AddConfigurationStore(options =>  {   options.ConfigureDbContext = builder =>    builder.UseSqlServer(connectionString,     sql => sql.MigrationsAssembly(migrationsAssembly));  })  .AddOperationalStore(options =>  {   options.ConfigureDbContext = builder =>    builder.UseSqlServer(connectionString,     sql => sql.MigrationsAssembly(migrationsAssembly));   options.EnableTokenCleanup = true;   options.TokenCleanupInterval = 30;  });}

并在Startup.cs文件里新增一個方法InitializeDatabase(初始化數據庫):

/// <summary>/// 初始數據庫/// </summary>/// <param name="app"></param>private void InitializeDatabase(IApplicationBuilder app){ using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) {  serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();//執行數據庫遷移  serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();  var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();  context.Database.Migrate();  if (!context.Clients.Any())  {   foreach (var client in Config.GetClients())//循環添加 我們直接添加的 5002、5003 客戶端   {    context.Clients.Add(client.ToEntity());   }   context.SaveChanges();  }   if (!context.IdentityResources.Any())    {     foreach (var resource in Config.GetIdentityResources())     {      context.IdentityResources.Add(resource.ToEntity());     }     context.SaveChanges();    }   if (!context.ApiResources.Any())    {     foreach (var resource in Config.GetApiResources())     {      context.ApiResources.Add(resource.ToEntity());     }     context.SaveChanges();    } }}

修改Configure方法:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env) {  //初始化數據  InitializeDatabase(app);  if (env.IsDevelopment())  {   app.UseDeveloperExceptionPage();   app.UseBrowserLink();   app.UseDatabaseErrorPage();  }  else  {   app.UseExceptionHandler("/Home/Error");  }  app.UseStaticFiles();  app.UseIdentityServer();  app.UseMvc(routes =>  {   routes.MapRoute(    name: "default",    template: "{controller=Home}/{action=Index}/{id?}");  }); }

然后新建一個AccountController控制器,分別實現注冊、登錄、登出等。

新建一個ConsentController控制器用于Client回調。

然后在Client的Startup.cs類里修改ConfigureServices方法:

public void ConfigureServices(IServiceCollection services){ services.AddMvc(); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(options => {  options.DefaultScheme = "Cookies";  options.DefaultChallengeScheme = "oidc"; }).AddCookie("Cookies").AddOpenIdConnect("oidc", options => {  options.SignInScheme = "Cookies";  options.Authority = "http://localhost:5001";  options.RequireHttpsMetadata = false;  options.ClientId = "mvc2";  options.ClientSecret = "secret";  options.ResponseType = "code id_token";  options.SaveTokens = true;  options.GetClaimsFromUserInfoEndpoint = true;  options.Scope.Add("api1");  options.Scope.Add("offline_access"); });}

c#,單點登錄

對于Client的身份認證就簡單了:

[Authorize]//身份認證public IActionResult Index(){ return View();}/// <summary>/// 登出/// </summary>/// <returns></returns>public async Task<IActionResult> Logout(){ await HttpContext.SignOutAsync("Cookies"); await HttpContext.SignOutAsync("oidc"); return View("Index");}

效果圖:


c#,單點登錄

源碼地址(demo可配置數據庫連接后直接運行)

https://github.com/zhaopeiym/BlogDemoCode/tree/master/sso(%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95)

總結

以上所述是小編給大家介紹的C#中單點登錄的原理和使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 叙永县| 梅州市| 祁门县| 易门县| 江永县| 平塘县| 文山县| 时尚| 宜州市| 房产| 新巴尔虎左旗| 旺苍县| 新蔡县| 星子县| 永修县| 富顺县| 怀远县| 荃湾区| 普格县| 瑞安市| 安顺市| 会宁县| 阳东县| 陇西县| 铁岭县| 五华县| 成武县| 兴文县| 三门县| 根河市| 桃园县| 岳阳县| 和田县| 沙坪坝区| 怀化市| 时尚| 鲁甸县| 开平市| 莲花县| 呼图壁县| 赤城县|