コード例 #1
0
        public TokenDto GenerateJWTToken(TTSUserDetailsByEmail user, Payload payload)
        {
            try
            {
                var Role               = user.designation;
                var BUName             = "NULL";
                var key                = configuration.GetValue <string>("SecretKey");
                var symmetricToken     = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
                var SigningCredentials = new SigningCredentials(symmetricToken, SecurityAlgorithms.HmacSha256Signature);
                var fullName           = user.firstName + " " + user.lastName;
                var claims             = new List <Claim>();
                claims.Add(new Claim("Email", payload.Email));
                claims.Add(new Claim("ImageUrl", payload.Picture));
                claims.Add(new Claim("Name", fullName));
                claims.Add(new Claim("UserId", user.userId.ToString()));
                claims.Add(new Claim("RoleId", Role.id.ToString()));
                claims.Add(new Claim("Role", Role.name));
                claims.Add(new Claim("BusinessUnitId", user.businessUnitId.ToString()));
                claims.Add(new Claim("BusinessUnit", BUName));
                claims.Add(new Claim("PhoneNo", user.mobileNo));
                var tokenDescriptor = new JwtSecurityToken(
                    issuer: "CabOAdmin",
                    audience: "CabOUser",

                    signingCredentials: SigningCredentials,
                    expires: DateTime.Now.AddHours(2),
                    claims: claims
                    );
                var token = new JwtSecurityTokenHandler().WriteToken(tokenDescriptor);


                TokenDto returnToken = new TokenDto();
                returnToken.token = token;
                return(returnToken);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #2
0
        public async Task <IActionResult> POSTAsync([FromBody] TokenDto value)
        {
            try
            {
                GoogleJsonWebSignature.ValidationSettings settings = new GoogleJsonWebSignature.ValidationSettings();
                //settings.Audience = new List<string>() { "781444592241-5l8d33p832p4muissesek9gvr74p08rk.apps.googleusercontent.com" };
                settings.Audience = new List <string>()
                {
                    "491676760446-n2786agrpa456hcr6cfns835t130glnm.apps.googleusercontent.com"
                };
                GoogleJsonWebSignature.Payload payload = await GoogleJsonWebSignature.ValidateAsync(value.token, settings);

                if (payload.EmailVerified)
                {
                    Payload userPayload = new Payload
                    {
                        Name          = payload.Name,
                        Email         = payload.Email,
                        EmailVerified = payload.EmailVerified,
                        Picture       = payload.Picture
                    };
                    TTSUserDetailsByEmail user = loginService.LoginCheck(payload.Email).Result;
                    if (user != null)
                    {
                        var token = loginService.GenerateJWTToken(user, userPayload);
                        return(Ok(token));
                    }
                }
                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(StatusCode(500));

                throw;
            }
        }