ClaimsIdentity 身份实现

IClaimsIdentityFactory : 

    /// <summary>
    ///  Interface for creating a ClaimsIdentity from an user
    /// </summary>
    public interface IClaimsIdentityFactory
    {
        Task<ClaimsIdentity> CreateAsync(Users user, string authenticationType);
    }

ClaimsIdentityFactory:


    /// <summary>
    /// Creates a ClaimsIdentity from a User
    /// </summary>
    public class ClaimsIdentityFactory : IClaimsIdentityFactory
    {
        const string IdentityProviderClaimType = "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider";
        const string DefaultIdentityProviderClaimValue = "ASP.NET Identity";

        /// <summary>
        ///     Claim type used for role claims
        /// </summary>
        public string RoleClaimType { get; set; }
        /// <summary>
        ///     Claim type used for the user name
        /// </summary>
        public string UserNameClaimType { get; set; }
        /// <summary>
        ///     Claim type used for the user id
        /// </summary>
        public string UserIdClaimType { get; set; }
        /// <summary>
        ///     Claim type used for the user security stamp
        /// </summary>
        public string SecurityStampClaimType { get; set; }


        public ClaimsIdentityFactory()
        {
            this.RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
            this.UserIdClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier";
            this.UserNameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
            this.SecurityStampClaimType = "AspNet.Identity.SecurityStamp";
        }

        public Task<ClaimsIdentity> CreateAsync(Users user, string authenticationType)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            ClaimsIdentity claimsIdentity = new ClaimsIdentity(authenticationType, this.UserNameClaimType, this.RoleClaimType);
            claimsIdentity.AddClaim(new Claim(this.UserIdClaimType, user.Id.ToString(), "http://www.w3.org/2001/XMLSchema#string"));
            claimsIdentity.AddClaim(new Claim(this.UserNameClaimType, user.UserName, "http://www.w3.org/2001/XMLSchema#string"));
            claimsIdentity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"));
            // if support securitystamp
            claimsIdentity.AddClaim(new Claim(this.SecurityStampClaimType, user.SecurityStamp));
            // if support user role 
            claimsIdentity.AddClaim(new Claim(this.RoleClaimType, user.Role.ToString()));

            return Task.FromResult(claimsIdentity);
        }
    }

已禁用评论。