资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

.netcore1.0如何实现单点登录负载多服务器-创新互联

这篇文章将为大家详细讲解有关.net core 1.0如何实现单点登录负载多服务器,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

成都创新互联从2013年开始,是专业互联网技术服务公司,拥有项目网站设计、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元宁波做网站,已为上家服务,为宁波各地企业和个人服务,联系电话:13518219792

实现方法

我们需要先封装一个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 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:\keys\key.xml 这个文件路径可以更改,还有就是也可用共享目录或数据库来实现统一管理

关于“.net core 1.0如何实现单点登录负载多服务器”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


当前标题:.netcore1.0如何实现单点登录负载多服务器-创新互联
分享地址:http://cdkjz.cn/article/cdegso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220