Пример #1
0
        public async Task <string> GenerateToken(long accountId)
        {
            ClaimsIdentity identity = await membershipProvider.GetUserClaims(accountId); // Role, email etc.

            var dateTimeNow = DateTime.UtcNow;

            var jwt = new JwtSecurityToken(
                issuer: Properties.Settings.Default.Issuer,
                audience: Properties.Settings.Default.Audience,
                notBefore: dateTimeNow,
                claims: identity.Claims,
                expires: new DateTime(2017, 09, 29),    //dateTimeNow.Add(TimeSpan.FromMinutes(Properties.Settings.Default.TokenLifeTime)), // + 5 minutes by default claim time
                signingCredentials: new SigningCredentials(GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256));

            return(new JwtSecurityTokenHandler().WriteToken(jwt));
        }
Пример #2
0
        public string GenerateJwtTokenAsync(string userEmail, string password)
        {
            if (!String.IsNullOrEmpty(password))
            {
                if (!_membershipProvider.VerifyUserPassword(userEmail, password))
                {
                    return("Wrong access");
                }
            }

            List <Claim> claims = _membershipProvider.GetUserClaims(userEmail);

            string publicAndPrivateKey = _rsaProvider.GetPrivateAndPublicKeyAsync();

            if (publicAndPrivateKey == null)
            {
                return("RSA key error");
            }

            RSACryptoServiceProvider publicAndPrivate = new RSACryptoServiceProvider();

            publicAndPrivate.FromXmlString(publicAndPrivateKey);

            JwtSecurityToken jwtToken = new JwtSecurityToken
                                        (
                issuer: "Any",
                audience: "Any",
                claims: claims,
                signingCredentials: new SigningCredentials(new RsaSecurityKey(publicAndPrivate), SecurityAlgorithms.RsaSha256Signature),
                expires: DateTime.Now.AddDays(30)
                                        );

            JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
            string tokenString = tokenHandler.WriteToken(jwtToken);

            return(tokenString);
        }