コード例 #1
0
        public async Task <IActionResult> Get([FromForm] string applicationUser)
        {
            var identity = await GetClaimsIdentity(applicationUser);

            if (identity == null)
            {
                _logger.LogInformation($"Invalid username ({applicationUser }) ");
                return(BadRequest("Invalid credentials"));
            }

            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, applicationUser),
                new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGeneration()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
                identity.FindFirst("DisneyCharacter")
            };

            // Create the JWT security token and encode it.
            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);

            // Serialize and return the response
            var response = new
            {
                access_token = encodedJwt,
                expires_in   = (int)_jwtOptions.ValidFor.TotalSeconds
            };

            var json = JsonConvert.SerializeObject(response, _serializerSettings);

            return(new OkObjectResult(json));
        }