public async Task <string> CreateTokenAync(string userId)
        {
            var userDetails = await _userApplication.GetAllUserDetailsAsync(userId);

            if (userDetails == null)
            {
                throw new Exception();
            }

            var userRoles = await _roleApplication.GetRolesByUserAsync(await _userApplication.GetUserByIdAsync(userId));

            if (userRoles == null)
            {
                throw new Exception();
            }

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, userDetails.Id.ToString()),
                new Claim(ClaimTypes.Name, userDetails.UserName),
                new Claim(ClaimTypes.Email, userDetails.Email),
                new Claim(ClaimTypes.MobilePhone, userDetails.PhoneNumber ?? ""),
                new Claim(ClaimTypes.GivenName, userDetails.FirstName),
                new Claim(ClaimTypes.Surname, userDetails.LastName),
                new Claim("AccessLevel", userDetails.AccessLevelTitle),
                new Claim("Date", userDetails.Date.ToString("yyyy/MM/dd HH:mm:ss")),
            };

            // اسم سطح دسترسی های کاربر را به کلیم اضافه میکند
            // ممکنه یک سطح دسترسی باشه یا چندتا فرقی نمیکند
            claims.AddRange(userRoles.Select(role => new Claim(ClaimsIdentity.DefaultRoleClaimType, role)));


            // ایجاد توکن برای ورود به سایت
            var key             = Encoding.ASCII.GetBytes(AuthConst.SecretCode);
            var tokenDescreptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
                Issuer             = AuthConst.Issuer,
                Audience           = AuthConst.Audience,
                IssuedAt           = DateTime.Now,
                Expires            = DateTime.Now.AddHours(48)
            };

            var    securityToken  = new JwtSecurityTokenHandler().CreateToken(tokenDescreptor);
            string generatedToken = "Bearer " + new JwtSecurityTokenHandler().WriteToken(securityToken);

            return(generatedToken.AesEncrypt(AuthConst.SecretKey));
        }
示例#2
0
        public async Task <string> CreateTokenAync(string UserId)
        {
            var _UserDetails = await _UserApplication.GetAllUserDetailsAsync(UserId);

            if (_UserDetails == null)
            {
                throw new Exception();
            }

            var _UserRoles = await _RoleApplication.GetRolesByUserAsync(await _UserApplication.GetUserByIdAsync(UserId));

            if (_UserDetails == null)
            {
                throw new Exception();
            }

            var Claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, _UserDetails.Id.ToString()),
                new Claim(ClaimTypes.Name, _UserDetails.UserName),
                new Claim(ClaimTypes.Email, _UserDetails.Email),
                new Claim(ClaimTypes.MobilePhone, _UserDetails.PhoneNumber ?? ""),
                new Claim(ClaimTypes.GivenName, _UserDetails.FirstName),
                new Claim(ClaimTypes.Surname, _UserDetails.LastName),
                new Claim("AccessLevel", _UserDetails.AccessLevelTitle),
                new Claim("Date", _UserDetails.Date.ToString("yyyy/MM/dd HH:mm:ss")),
            };

            Claims.AddRange(_UserRoles.Select(role => new Claim(ClaimsIdentity.DefaultRoleClaimType, role)));

            var _Key            = Encoding.ASCII.GetBytes(AuthConst.SecretCode);
            var TokenDescreptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(Claims),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(_Key), SecurityAlgorithms.HmacSha256Signature),
                Issuer             = AuthConst.Issuer,
                Audience           = AuthConst.Audience,
                IssuedAt           = DateTime.Now,
                Expires            = DateTime.Now.AddHours(48)
            };

            var    _SecurityToken  = new JwtSecurityTokenHandler().CreateToken(TokenDescreptor);
            string _GeneratedToken = "Bearer " + new JwtSecurityTokenHandler().WriteToken(_SecurityToken);

            return(_GeneratedToken.AesEncrypt(AuthConst.SecretKey));
        }