/// <summary>
        /// The Method Add ClaimsIdentity Properties.
        /// </summary>
        /// <param name="username">The identity user.</param>
        public EmployeeViewModel ManageClaimsIdentity(LoginViewModel login)
        {
            Hremployee data = _unitOfWork.GetRepository <Hremployee>().Get(x => x.Aduser == login.Username).FirstOrDefault();

            if (data == null)
            {
                throw new ArgumentNullException(ConstantValue.HrEmployeeArgumentNullExceptionMessage);
            }
            var result = new EmployeeViewModel
            {
                EmpNo       = data.EmpNo,
                FirstNameTH = data.FirstnameTh,
                LastNameTH  = data.LastnameTh,
                AdUser      = data.Aduser,
                ComCode     = data.ComCode,
                OrgId       = data.OrgId,
                PositionId  = data.PositionId
            };
            var sapComCode = _unitOfWork.GetRepository <Hrcompany>().GetCache(x => x.ComCode == data.ComCode).FirstOrDefault()?.SapcomCode;
            var roleList   = _roleBll.GetCompositeRoleItemByAdUser(login.Username);

            _identity = new ClaimsIdentity();
            _identity.AddClaim(new Claim(ClaimTypes.Name, data.Aduser));
            _identity.AddClaim(new Claim(ConstantValue.ClamisEncrypt, UtilityService.EncryptString(login.Password, _config.EncryptionKey)));
            _identity.AddClaim(new Claim(ConstantValue.ClamisEmpNo, data.EmpNo));
            _identity.AddClaim(new Claim(ConstantValue.ClamisName, string.Format(ConstantValue.EmpTemplate, data.FirstnameTh, data.LastnameTh)));
            _identity.AddClaim(new Claim(ConstantValue.ClamisOrg, data.OrgId));
            _identity.AddClaim(new Claim(ConstantValue.ClamisPosition, data.PositionId));
            _identity.AddClaim(new Claim(ConstantValue.ClamisComCode, sapComCode));
            //Add authority company
            var authorityList = _unitOfWork.GetRepository <AuthorityCompany>().GetCache(x => x.AdUser == data.Aduser);

            foreach (var item in authorityList)
            {
                _identity.AddClaim(new Claim(ConstantValue.ClamisComCode, item.ComCode));
            }
            //Add purchase org
            var purOrgs = _unitOfWork.GetRepository <PurchaseOrgItem>().GetCache(x => x.AdUser == data.Aduser);

            foreach (var item in purOrgs)
            {
                _identity.AddClaim(new Claim(ConstantValue.ClamisPurchasing, item.PuchaseOrg));
            }
            //Add role
            foreach (var item in roleList)
            {
                _identity.AddClaim(new Claim(ClaimTypes.Role, item.RoleMenu));
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Initial Mockup HttpContext inject to test.
        /// </summary>
        /// <returns></returns>
        private HttpContextAccessor InitialHttpContext()
        {
            var httpContextAccessor = new HttpContextAccessor();
            var httpContext         = new DefaultHttpContext();

            var identity = new ClaimsIdentity();

            identity.AddClaim(new Claim(ClaimTypes.Name, "BOONRAWD_LOCAL\\ds01"));
            identity.AddClaim(new Claim(ConstantValue.ClamisEncrypt, UtilityService.EncryptString("hw_2931", "UlZaR1JVNURVbGxRVkVsUFRrdEZXUT09")));
            identity.AddClaim(new Claim(ConstantValue.ClamisEmpNo, "001754"));
            identity.AddClaim(new Claim(ConstantValue.ClamisName, string.Format(ConstantValue.EmpTemplate, "สัญชัย", "ต้นพุดซา")));
            identity.AddClaim(new Claim(ConstantValue.ClamisOrg, "10001416"));
            identity.AddClaim(new Claim(ConstantValue.ClamisPosition, "20000641"));
            identity.AddClaim(new Claim(ConstantValue.ClamisComCode, "1600"));
            identity.AddClaim(new Claim(ConstantValue.ClamisPurchasing, "1600"));
            var user = new GenericPrincipal(new ClaimsIdentity(identity), new string[] { "ADMIN" });

            httpContext.User = user;

            httpContextAccessor.HttpContext = httpContext;
            return(httpContextAccessor);
        }