Ejemplo n.º 1
0
        private async Task GenerateToken(HttpContext context, string username, string password)
        {
            AuthenticationUser user = await _userManager.FindByEmailAsync(username);

            ClaimsIdentity identity = await GetIdentity(user, username, password);

            if (identity == null)
            {
                context.Response.StatusCode = 200;
                await context.Response.WriteAsync(
                    SeriaizeErrorResponse("Invalid username or password."));

                return;
            }

            identity.AddClaims(await GetUserClaims(username, user));

            DateTime now = DateTime.UtcNow;
            JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();

            JwtSecurityToken jwt = handler.CreateJwtSecurityToken(subject: identity,
                                                                  signingCredentials: _options.SigningCredentials,
                                                                  issuer: _options.Issuer,
                                                                  audience: _options.Audience,
                                                                  notBefore: now,
                                                                  expires: now.Add(_options.Expiration));

            string encodedJwt = handler.WriteToken(jwt);

            var response = new TokenResponce()
            {
                AccessToken = encodedJwt,
                ExpiresIn   = (int)_options.Expiration.TotalSeconds
            };

            context.Response.ContentType = "application/json";
            await context.Response.WriteAsync(SeriaizeOkResponse(response));
        }
Ejemplo n.º 2
0
 private string SeriaizeOkResponse(TokenResponce response)
 {
     return(ServiceObjectResponse(new OkServiceResponse <TokenResponce>(response)));
 }