Beispiel #1
0
        public static ClaimsPrincipal GetUser(EmployeeRole role)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimsIdentity.DefaultNameClaimType, role.ToString()),
                new Claim(ClaimsIdentity.DefaultRoleClaimType, role.ToString())
            };
            // создаем объект ClaimsIdentity
            ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);

            // установка аутентификационных куки
            return(new ClaimsPrincipal(id));
        }
 public EmployeeDataModel CreateEmployeeDataModel()
 {
     return(new EmployeeDataModel
     {
         Name = _name,
         WarehouseId = _warehouseId,
         Role = _role.ToString(),
         Ext = _ext
     });
 }
        public static Mock <HttpContext> Build(EmployeeRole role)
        {
            // Create a mock HttpContext
            var contextMock = new Mock <HttpContext>();

            // Mocking HttpContext
            contextMock.SetupGet(ctx => ctx.User).Returns(TestFunctions.GetUser(role));
            contextMock.SetupGet(ctx => ctx.User.Identity.Name).Returns(role.ToString());
            contextMock.SetupGet(ctx => ctx.User.Identity.IsAuthenticated).Returns(true);
            contextMock.Setup(ctx => ctx.User.IsInRole(EmployeeRole.Admin.ToString())).Returns(role.Equals(EmployeeRole.Admin));
            return(contextMock);
        }
Beispiel #4
0
        private string GenerateEncodedToken(string userName, EmployeeRole userRole)
        {
            var claimsIdentity = CreateClaimsIdentity(userName, userRole.ToString().ToLower());
            var claims         = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, userName),
                new Claim(JwtRegisteredClaimNames.Jti, _jwtOptions.JtiGenerator),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
                claimsIdentity.FindFirst(Constants.ClaimTypes.EmployeeRole)
            };

            var jwt = new JwtSecurityToken(
                issuer: _jwtOptions.Issuer,
                audience: _jwtOptions.Audience,
                claims: claims,
                notBefore: _jwtOptions.NotBefore,
                expires: _jwtOptions.Expiration,
                signingCredentials: _jwtOptions.SigningCredentials);

            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            return(encodedJwt);
        }