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: