来自  资质荣誉 2019-09-23 19:29 的文章
当前位置: 澳门太阳娱乐手机登录 > 资质荣誉 > 正文

Asp.net Boilerplate之AbpSession扩充

现阶段Abp版本1.4.2,项目连串为MVC5.

以属性的格局扩充AbpSession,并在"记住自个儿"后,下一次机关登陆也能得到到增加属性的值,版权归"角落的白板报"全体!

step1.添加IAbpSessionExtensions接口:

    /// <summary>    /// IAbpSession Extensions 登录时"记住我"保存Cookie,也就是说关闭后再打开不重新登录的话,取得到值.    /// </summary>    public interface IAbpSessionExtensions : IAbpSession    {        string UserName { get; }        string EmailAddress { get; }        string Name { get; }        string FullName { get; }        string Surname { get; }    }

step2.加多兑现类AbpSessionExtensions:

/// <summary>    /// IAbpSession Extensions    /// </summary>    public class AbpSessionExtensions : ClaimsAbpSession, IAbpSessionExtensions    {

/// 如果是之前的版本,此处的构造函数会有所不同,只要把这个构造函数删除,再根据Vs的提示生成默认实现就可以.

      public AbpSessionExtensions(IPrincipalAccessor principalAccessor,

        IMultiTenancyConfig multiTenancy, ITenantResolver tenantResolver,

        IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider)
          : base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider)
      {
      }

     public string EmailAddress => GetKeyValue(ClaimTypes.Email);        public string Surname => GetKeyValue(ClaimTypes.Surname);        public string Name => GetKeyValue(ClaimTypes.Name);        public string UserName => GetKeyValue(EnglishConsts.ClaimTypes.UserName);        public string FullName => GetKeyValue(EnglishConsts.ClaimTypes.FullName);        private string GetKeyValue(string key)        {            var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;            if (claimsPrincipal == null)            {                return null;            }            var claim = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == key);            if (string.IsNullOrEmpty(claim?.Value))            {                return null;            }            return claim.Value;        }    }

step3.在Mvc调节器基类(项目名称ControllerBase)里增多AbpSession属性:

        //IAbpSession Extensions        public new IAbpSessionExtensions AbpSession { get; set; }

step4.在Account调节器的SignInAsync方法里学有所成登入后(AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity),增多供给扩张的质量:

            //IAbpSession Extensions            identity.AddClaim(new Claim(ClaimTypes.Email, user.EmailAddress));            identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));            identity.AddClaim(new Claim(ClaimTypes.Surname, user.Surname));
       //这个常量自己去定义            identity.AddClaim(new Claim(EnglishConsts.ClaimTypes.UserName, user.UserName));            identity.AddClaim(new Claim(EnglishConsts.ClaimTypes.FullName, user.FullName));

step5.这里本身定义了多个自定义常量,加在了项目名称Consts常量定义文件里:

 public class EnglishConsts    {        public const string LocalizationSourceName = "English";        public class ClaimTypes        {            public const string UserName = "UserName";            public const string FullName = "FullName";        }    }

那样就大功告成了,登入或后一次活动登入之后,在代码里的智能提示里就能够见到扩大的品质:

图片 1

而且值也保留在了中间:

图片 2

本文由澳门太阳娱乐手机登录发布于 资质荣誉,转载请注明出处:Asp.net Boilerplate之AbpSession扩充

关键词: