Exemple #1
0
        //
        /// <summary>
        /// Create a 'to string'.
        /// </summary>
        public override string ToString()
        {
            //
            StringBuilder _return = new StringBuilder("record:[");

            _return.AppendFormat("Id: {0}, ", Id);
            _return.AppendFormat("UserName: {0}, ", UserName);
            _return.AppendFormat("Email: {0}, ", Email);
            _return.AppendFormat("FirstName: {0}, ", FirstName);
            _return.AppendFormat("LastName: {0}, ", LastName);
            _return.AppendFormat("FullName: {0}, ", FullName);
            _return.AppendFormat("UserNicName: {0}, ", UserNicName);
            _return.AppendFormat("CompanyId: {0}, ", CompanyId.ToString());
            _return.AppendFormat("CreateDate: {0}, ", CreateDate.ToString());
            _return.AppendFormat("EmailConfirmed: {0}, ", EmailConfirmed.ToString());
            _return.AppendFormat("PhoneNumber: {0}, ", PhoneNumber);
            _return.AppendFormat("PhoneNumberConfirmed: {0}, ", PhoneNumberConfirmed.ToString());
            _return.AppendFormat("TwoFactorEnabled: {0}, ", TwoFactorEnabled.ToString());
            if (LockoutEndDateUtc.HasValue)
            {
                _return.AppendFormat("LockoutEndDateUtc: {0}, ", LockoutEndDateUtc.ToString());
            }
            else
            {
                _return.AppendFormat("/LockoutEndDateUtc/, ");
            }
            _return.AppendFormat("LockoutEnabled: {0}, ", LockoutEnabled.ToString());
            _return.AppendFormat("AccessFailedCount: {0}]", AccessFailedCount.ToString());
            return(_return.ToString());
        }
Exemple #2
0
        public static async Task GetProfileDataAsync_GivenAnApplicationUserExistsWithOrganisationFunctionBuyer_ReturnsExpectedClaimList()
        {
            var expectedApplicationUser = ApplicationUserBuilder
                                          .Create()
                                          .WithEmailAddress("*****@*****.**")
                                          .WithFirstName("Bob")
                                          .WithLastName("Smith")
                                          .WithOrganisationFunction(OrganisationFunction.Buyer)
                                          .Build();

            Mock <IUsersRepository> applicationUserRepositoryMock = new Mock <IUsersRepository>();

            applicationUserRepositoryMock
            .Setup(r => r.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(expectedApplicationUser);

            var expectedOrganisation = OrganisationBuilder
                                       .Create()
                                       .Build();

            Mock <IOrganisationRepository> organisationRepositoryMock = new Mock <IOrganisationRepository>();

            organisationRepositoryMock
            .Setup(r => r.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync(expectedOrganisation);

            var sut = ProfileServiceBuilder
                      .Create()
                      .WithUserRepository(applicationUserRepositoryMock.Object)
                      .WithOrganisationRepository(organisationRepositoryMock.Object)
                      .Build();

            var profileDataRequestContext = ProfileDataRequestContextBuilder
                                            .Create()
                                            .WithSubjectId(expectedApplicationUser.Id)
                                            .Build();

            await sut.GetProfileDataAsync(profileDataRequestContext);

            var expected = new List <(string, string)>
            {
                (Subject, expectedApplicationUser.Id),
                (PreferredUserName, expectedApplicationUser.UserName),
                (JwtRegisteredClaimNames.UniqueName, expectedApplicationUser.UserName),
                (GivenName, expectedApplicationUser.FirstName),
                (FamilyName, expectedApplicationUser.LastName),
                (Name, $"{expectedApplicationUser.FirstName} {expectedApplicationUser.LastName}"),
                (Email, expectedApplicationUser.Email),
                (EmailVerified, expectedApplicationUser.EmailConfirmed.ToString(CultureInfo.CurrentCulture).ToLowerInvariant()),
                (ApplicationClaimTypes.PrimaryOrganisationId, expectedApplicationUser.PrimaryOrganisationId.ToString()),
                (ApplicationClaimTypes.PrimaryOrganisationName, expectedOrganisation.Name),
                (ApplicationClaimTypes.OrganisationFunction, expectedApplicationUser.OrganisationFunction.DisplayName),
                (ApplicationClaimTypes.Ordering, Manage),
                (ApplicationClaimTypes.Organisation, View),
            };

            var actual = profileDataRequestContext.IssuedClaims.Select(item => (item.Type, item.Value));

            actual.Should().BeEquivalentTo(expected);
        }
Exemple #3
0
        public async Task <ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            // Add custom user claims here
            userIdentity.AddClaim(new Claim(CustomClaims.EmailConfirmed, EmailConfirmed.ToString()));

            return(userIdentity);
        }
        public async Task <ClaimsIdentity> GenerateUserIdentityAsync(UserManager <ApplicationUser> manager, string authenticationType)
        {
            var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);

            // Add custom user claims here
            userIdentity.AddClaim(new Claim("email_confirmed", EmailConfirmed.ToString()));

            userIdentity.AddClaim(new Claim("first_name", FirstName ?? ""));
            userIdentity.AddClaim(new Claim("last_name", LastName ?? ""));

            return(userIdentity);
        }
Exemple #5
0
        public List <Claim> ToClaims()
        {
            var claims = new List <Claim>();

            claims.Add(new Claim("username", UserName));
            claims.Add(new Claim("creation-date", CreationDate.ToString("o")));
            claims.Add(new Claim("last-seen", LastSeen.ToString("o")));
            claims.Add(new Claim("last-edit", LastEdit.ToString("o")));
            claims.Add(new Claim("account-type", AccountType.ToString()));
            claims.Add(new Claim("account-status", AccountStatus.ToString()));
            claims.Add(new Claim("recovery-email", Email ?? string.Empty));
            claims.Add(new Claim("recovery-verified", EmailConfirmed.ToString()));
            claims.Add(new Claim("2fa-enabled", TwoFactorEnabled.ToString()));
            claims.Add(new Claim("pgp-public-key", PGPPublicKey ?? string.Empty));
            return(claims);
        }
Exemple #6
0
        //------------------------------------------------------------------------------------------------------
        #region GenerateUserIdentityAsync

        public async Task <ClaimsIdentity> GenerateUserIdentityAsync(UserManager <User, string> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            // Add custom user claims here

            userIdentity.AddClaims(new List <Claim>()
            {
                //new Claim("OrgId",OrganizationId),
                //new Claim("Id",Id),
                new Claim(UserClaims.Avatar.ToString(), GetAvatar()),
                new Claim("Name", GetName()),
                new Claim("FullName", FirstName + " " + LastName),
                new Claim("EmailConfirmed", EmailConfirmed.ToString()),
                new Claim("PhoneNumberConfirmed", PhoneNumberConfirmed.ToString()),
            });

            return(userIdentity);
        }
Exemple #7
0
        //
        /// <summary>
        /// Create a 'to string'.
        /// </summary>
        public override string ToString()
        {
            //
            StringBuilder _return = new StringBuilder("record:[");

            _return.AppendFormat("Id: {0}, ", Id);
            _return.AppendFormat("UserName: {0}, ", UserName);
            _return.AppendFormat("Email: {0}, ", Email);
            _return.AppendFormat("FirstName: {0}, ", FirstName);
            _return.AppendFormat("LastName: {0}, ", LastName);
            _return.AppendFormat("FullName: {0}, ", FullName);
            _return.AppendFormat("UserNicName: {0}, ", UserNicName);
            _return.AppendFormat("CompanyId: {0}, ", CompanyId.ToString());
            _return.AppendFormat("EmailConfirmed: {0}, ", EmailConfirmed.ToString());
            _return.AppendFormat("PhoneNumber: {0}, ", PhoneNumber);
            _return.AppendFormat("PhoneNumberConfirmed: {0}, ", PhoneNumberConfirmed.ToString());
            _return.AppendFormat("ServerShortName: {0}, ", ServerShortName);
            return(_return.ToString());
            //
        }
Exemple #8
0
        /// <summary>
        /// Transforms user properties into claims
        /// </summary>
        /// <returns></returns>
        public virtual ICollection <Claim> ToClaims()
        {
            var claims =
                new List <Claim> {
                new Claim(JwtClaimTypes.Subject, Id.ToString()),
                new Claim(JwtClaimTypes.Name, UserName),
                new Claim(ClaimTypes.Name, UserName),
            };

            if (Email != default)
            {
                claims.Add(new Claim(JwtClaimTypes.Email, Email));
                claims.Add(new Claim(JwtClaimTypes.EmailVerified, EmailConfirmed.ToString().ToLower()));
            }
            if (PhoneNumber != default)
            {
                claims.Add(new Claim(JwtClaimTypes.PhoneNumber, PhoneNumber));
                claims.Add(new Claim(JwtClaimTypes.PhoneNumberVerified, PhoneNumberConfirmed.ToString().ToLower()));
            }
            if (Organization != default)
            {
                claims.Add(new Claim(DomainClaimTypes.Organization, Email));
                claims.Add(new Claim(DomainClaimTypes.OrganizationConfirmed, OrganizationConfirmed.ToString().ToLower()));
                if (OrganizationAdmin)
                {
                    claims.Add(new Claim(DomainClaimTypes.OrganizationAdminFor, Organization));
                }
            }
            if (SuperAdmin)
            {
                claims.Add(new Claim(DomainClaimTypes.SuperAdmin, true.ToString()));
            }
            if (LockoutBegin <= DateTime.Now && LockoutEnd >= DateTime.Now)
            {
                claims.Add(new Claim(DomainClaimTypes.Locked, "true"));
            }

            return(claims);
        }
Exemple #9
0
        /// <summary>
        /// Generate user identity as an asynchronous operation.
        /// </summary>
        /// <param name="manager">The <see cref="ApplicationUser"/> manager.</param>
        /// <returns>
        /// <see cref="ClaimsIdentity"/>.
        /// </returns>
        public async Task <ClaimsIdentity> GenerateUserIdentityAsync(UserManager <ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            // Add custom user claims here
            if (FirstName != null && LastName != null)
            {
                userIdentity.AddClaim(new Claim("FirstName", FirstName));
                userIdentity.AddClaim(new Claim("LastName", LastName));
            }


            using (DbContexts.DbContext _db = new DbContexts.DbContext())
            {
                User u = _db.Users.FirstOrDefault(user => user.AspNetUserId == Id);
                if (u != null)
                {
                    userIdentity.AddClaim(new Claim("InternalUserId", u.UserId.ToString()));
                    userIdentity.AddClaim(new Claim("EmailConfirm", EmailConfirmed.ToString()));
                }
            }
            return(userIdentity);
        }