Asp.net Boilerplate之AbpSession擴展

當前Abp版本1.4.2,項目類型爲MVC5.session

以屬性的形式擴展AbpSession,並在"記住我"後,下次自動登陸也能獲取到擴展屬性的值,版權歸"角落的白板報"全部!ide

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:spa

/// <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)
      {
      }3d

     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屬性:code

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

step4.在Account控制器的SignInAsync方法裏成功登陸後(AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity),添加須要擴展的屬性:blog

            //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";
        }
    }

 

這樣就完成了,登陸或下次自動登陸以後,在代碼裏的智能提示裏就能夠看到擴展的屬性:ip

並且值也保存在了裏面:ci

相關文章
相關標籤/搜索