Asp.net Boilerplate-AbpSession extension,

Source: Internet
Author: User
Tags constant definition

Asp.net Boilerplate-AbpSession extension,

The project type is MVC5. the project type is 1.2.

Extend the AbpSession in the form of attributes, and after "Remember Me", the value of the extended attributes can be obtained in the next automatic login. The copyright belongs to "!

Step 1: add the IAbpSessionExtensions interface:

/// <summary>
     /// IAbpSession Extensions "Remember me" to save cookies when logging in, that is to say, if you close and reopen without logging in again, you will get the value.
     /// </ summary>
     public interface IAbpSessionExtensions: IAbpSession
     {
         string UserName {get;}
         string EmailAddress {get;}
         string Name {get;}
         string FullName {get;}
         string Surname {get;}
     }

Step 2: add the implementation class AbpSessionExtensions:

/// <summary>
    /// IAbpSession Extensions
    /// </summary>
    public class AbpSessionExtensions : ClaimsAbpSession, IAbpSessionExtensions
    {
        public AbpSessionExtensions(IMultiTenancyConfig multiTenancy) : base(multiTenancy)
        {

        }

        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. Add the AbpSession attribute to the Mvc controller base class (project name ControllerBase:

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

Step 4. Add attributes to be extended after successfully logging on (AuthenticationManager. SignIn (new AuthenticationProperties {IsPersistent = rememberMe}, identity) in the SignInAsync method of the Account controller:

   //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));
       //Define this constant yourself
            identity.AddClaim(new Claim(EnglishConsts.ClaimTypes.UserName, user.UserName));
            identity.AddClaim(new Claim(EnglishConsts.ClaimTypes.FullName, user.FullName)); 

Step 5. Here I have defined two custom constants and added them to the Project name Consts constant definition file:

 public class EnglishConsts
    {
        public const string LocalizationSourceName = "English";

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

 

This completes. After logging on or the next automatic login, you can see the extended attributes in the smart prompts in the Code:


And the value is saved in it:


 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.