栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

.net core 1.0 实现单点登录负载多服务器

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net core 1.0 实现单点登录负载多服务器

前言

  .net core 出来有一时间了,这段时间也一直在做技术准备,目前想做一个单点登录(SSO)系统,在这之前用.net时我用习惯了machineKey ,也顺手在.net core 中尝试了一上,结果发现不好使了,也不起作用,于是开始了网上学习。

实现方法

  功夫不负有心人,网上高人还是多,在github.com上面ISSUES中也有人在讨论此问题,于是找到代码尝试,结果实现了。

  直接上代码,我们需要先封装一个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:keyskey.xml";
    public virtual IReadOnlyCollection GetAllElements()
    {
      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:keyskey.xml 这个文件路径可以更改,还有就是也可用共享目录或数据库来实现统一管理

到此可以登录试一下。

以上所述是小编给大家介绍的.net core 1.0 实现单点登录负载多服务器的全部叙述,希望对大家有所帮助!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/56165.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号