예제 #1
0
        public static ClaimsIdentity Identity(this IAuthenticator authenticator, IUser user, TimeSpan?expiration = null)
        {
            if (user == null)
            {
                return(new ClaimsIdentity());
            }

            var issuer   = authenticator.Name;
            var identity = new CredentialIdentity(user.Name, authenticator.Name, issuer)
            {
                Label = user.FullName
            };

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString(), ClaimValueTypes.UInteger32, issuer, issuer, identity));

            if (!string.IsNullOrEmpty(user.Namespace))
            {
                identity.AddClaim(new Claim(ClaimNames.Namespace, user.Namespace, ClaimValueTypes.String, issuer, issuer, identity));
            }
            if (!string.IsNullOrEmpty(user.Description))
            {
                identity.AddClaim(new Claim(ClaimNames.Description, user.Description, ClaimValueTypes.String, issuer, issuer, identity));
            }
            if (!string.IsNullOrEmpty(user.Email))
            {
                identity.AddClaim(new Claim(ClaimTypes.Email, user.Email.ToString(), ClaimValueTypes.String, issuer, issuer, identity));
            }
            if (!string.IsNullOrEmpty(user.Phone))
            {
                identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.Phone.ToString(), ClaimValueTypes.String, issuer, issuer, identity));
            }

            identity.AddClaim(new Claim(ClaimNames.UserStatus, user.Status.ToString(), ClaimValueTypes.Integer32, issuer, issuer, identity));

            if (user.StatusTimestamp.HasValue)
            {
                identity.AddClaim(new Claim(ClaimNames.UserStatusTimestamp, user.StatusTimestamp.ToString(), ClaimValueTypes.DateTime, issuer, issuer, identity));
            }

            identity.AddClaim(new Claim(ClaimNames.Creation, user.Creation.ToString(), ClaimValueTypes.DateTime, issuer, issuer, identity));

            if (user.Modification.HasValue)
            {
                identity.AddClaim(new Claim(ClaimNames.Modification, user.Modification.ToString(), ClaimValueTypes.DateTime, issuer, issuer, identity));
            }

            if (expiration.HasValue && expiration.Value > TimeSpan.Zero)
            {
                identity.AddClaim(new Claim(ClaimTypes.Expiration, expiration.ToString(), expiration.Value.TotalHours > 24 ? ClaimValueTypes.YearMonthDuration : ClaimValueTypes.DaytimeDuration, issuer, issuer, identity));
            }

            return(identity);
        }
예제 #2
0
        public static ClaimsIdentity Identity(this IUser user, string scheme, string issuer, TimeSpan?expiration = null)
        {
            if (user == null)
            {
                return(new ClaimsIdentity());
            }

            var identity = new CredentialIdentity(user.Name, scheme, issuer)
            {
                Label = user.FullName
            };

            SetClaims(identity, user, expiration);

            return(identity);
        }