這篇文章將為大家詳細講解有關.net core 1.0如何實現(xiàn)單點登錄負載多服務器,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目網(wǎng)站設計、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元寧波做網(wǎng)站,已為上家服務,為寧波各地企業(yè)和個人服務,聯(lián)系電話:13518219792實現(xiàn)方法
我們需要先封裝一個XmlRepository,Key的格式如下:
2016-07-23T10:09:49.1888876Z 2016-07-23T10:09:49.1388521Z 2116-10-21T10:09:49.1388521Z WYgZNh/3dOKRYJ1OAhVqs56pWPMHei15Uj44DPLWbYUiCpNVEBwqDfYAUq/4jBKYrNoUbaRkGY5o/NZ6a2NTwA==
XmlRepository代碼:
public class CustomFileXmlRepository : IXmlRepository { private readonly string filePath = @"C:\keys\key.xml"; public virtual IReadOnlyCollectionGetAllElements() { return GetAllElementsCore().ToList().AsReadOnly(); } private IEnumerable GetAllElementsCore() { yield return XElement.Load(filePath); } public virtual void StoreElement(XElement element, string friendlyName) { if (element == null) { throw new ArgumentNullException(nameof(element)); } StoreElementCore(element, friendlyName); } private void StoreElementCore(XElement element, string filename) { } }
Startup代碼:
public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton(); services.AddDataProtection(configure => { configure.ApplicationDiscriminator = "Htw.Web"; }); // Add framework services. services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseCookieAuthentication(new CookieAuthenticationOptions() { AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme, LoginPath = new PathString("/Account/Unauthorized/"), AccessDeniedPath = new PathString("/Account/Forbidden/"), AutomaticAuthenticate = true, AutomaticChallenge = false, CookieHttpOnly = true, CookieName = "MyCookie", ExpireTimeSpan = TimeSpan.FromHours(2), #if !DEBUG CookieDomain="h.cn", #endif DataProtectionProvider = null }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }
登錄代碼:
public async void Login() { if (!HttpContext.User.Identities.Any(identity => identity.IsAuthenticated)) { var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "bob") }, CookieAuthenticationDefaults.AuthenticationScheme)); await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user); HttpContext.Response.ContentType = "text/plain"; await HttpContext.Response.WriteAsync("Hello First timer"); } else { HttpContext.Response.ContentType = "text/plain"; await HttpContext.Response.WriteAsync("Hello old timer"); } }
注意
C:\keys\key.xml 這個文件路徑可以更改,還有就是也可用共享目錄或數(shù)據(jù)庫來實現(xiàn)統(tǒng)一管理
關于“.net core 1.0如何實現(xiàn)單點登錄負載多服務器”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。