当前位置: 首页  >  ASP.NET  >  伪Session这样共享到Service

伪Session这样共享到Service TOP

在显示层用如下代码,把需要存储的值放到线程拥有者里,代码如下

         var claimsPrincipal = new  ClaimsPrincipal();
             var a =new  ClaimsIdentity();
             a.AddClaim(new Claim("UserId","150"));
            if (claimsPrincipal != null) claimsPrincipal.AddIdentity(a);
        Thread.CurrentPrincipal = claimsPrincipal;

在Service层调用的时候如下:

  var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
            var userId= claimsPrincipal.Claims.FirstOrDefault(c => c.Type == "UserId").Value;

请问这样把Seesion共享到Service可行吗?

Views:483   Posted at:2018-02-25
收藏 推荐 打印 | 录入:Admin | 阅读:0