Esempio n. 1
0
        /// <summary>
        /// Login user with addiitonal data
        /// </summary>
        /// <param name="principal"></param>
        /// <param name="user"></param>
        /// <param name="properties"></param>
        public void Login <T>(ClaimsPrincipal principal, T user, AuthenticationProperties properties = null) where T : class
        {
            if (principal == null)
            {
                throw new ArgumentNullException(nameof(principal));
            }

            if (user != null)
            {
                (principal.Identity as ClaimsIdentity)?.AddClaims(user.ExtractClaims());
                principal.AddClaim(ClaimTypes.UserData, user);
            }

            if (properties != null)
            {
                principal.AddClaim(PrincipalExtensions.AuthenticationPropertiesClaimName, properties);
            }

            var claimProperties = principal.GetClaim <AuthenticationProperties>(PrincipalExtensions.AuthenticationPropertiesClaimName);

            if (Context != null)
            {
                var scheme = principal.Identity.AuthenticationType ?? CookieAuthenticationDefaults.AuthenticationScheme;

                if (scheme == CookieAuthenticationDefaults.AuthenticationScheme)
                {
                    Context.SignInAsync(scheme, principal, claimProperties.To()).Wait();
                }
                Context.User = principal;
            }

            Thread.CurrentPrincipal = principal;
        }
        public void AddClaim()
        {
            // Arrange
            var componentUnderTest = new ClaimsPrincipal(new [] { new ClaimsIdentity() });

            // Act
            componentUnderTest.AddClaim("myType", "value1");

            // Assert
            var claim = componentUnderTest.Claims.First();

            claim.Type.ShouldHaveSameValueAs("myType");
            claim.Value.ShouldHaveSameValueAs("value1");
        }
Esempio n. 3
0
 /// <summary>
 /// Add the claim 'organizations' to the specified 'user'.
 /// </summary>
 /// <param name="user"></param>
 /// <param name="organizationIds"></param>
 /// <returns></returns>
 public static ClaimsPrincipal AddOrganization(this ClaimsPrincipal user, IEnumerable <int> organizationIds)
 {
     return(user.AddClaim("organizations", String.Join(",", organizationIds)));
 }
Esempio n. 4
0
        /// <summary>
        /// Add the claim 'organizations' to the specified 'user'.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="organizationId"></param>
        /// <returns></returns>
        public static ClaimsPrincipal AddOrganization(this ClaimsPrincipal user, params int[] organizationId)
        {
            var organizations = String.Join(",", organizationId);

            return(user.AddClaim("organizations", organizations));
        }
Esempio n. 5
0
 /// <summary>
 /// Add the claim 'agencies' to the specified 'user'.
 /// </summary>
 /// <param name="user"></param>
 /// <param name="agencyIds"></param>
 /// <returns></returns>
 public static ClaimsPrincipal AddAgency(this ClaimsPrincipal user, IEnumerable <int> agencyIds)
 {
     return(user.AddClaim("agencies", String.Join(",", agencyIds)));
 }
Esempio n. 6
0
        /// <summary>
        /// Add the claim 'agencies' to the specified 'user'.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="agencyId"></param>
        /// <returns></returns>
        public static ClaimsPrincipal AddAgency(this ClaimsPrincipal user, params int[] agencyId)
        {
            var agencies = String.Join(",", agencyId);

            return(user.AddClaim("agencies", agencies));
        }