public LoginController(IUserService userService, IJwtSecurityTokenService jwtSecurityTokenService, IMemoryCache cache, IUserRoleService userRole)
 {
     this.userRole                = userRole;
     this.userService             = userService;
     this.jwtSecurityTokenService = jwtSecurityTokenService;
     this.cache = cache;
 }
Beispiel #2
0
 public UserService(IBaseUtilitiesProvider baseUtilitiesProvider,
                    IPasswordService passwordService,
                    IJwtSecurityTokenService jwtSecurityTokenService,
                    IUserValidationService userValidationService) : base(baseUtilitiesProvider)
 {
     this.passwordService         = passwordService;
     this.jwtSecurityTokenService = jwtSecurityTokenService;
     this.userValidationService   = userValidationService;
 }
Beispiel #3
0
 public TokenController(
     SignInManager <User> signInManager,
     UserManager <User> userManager,
     IJwtSecurityTokenService jwt)
 {
     _signInManager = signInManager;
     _userManager   = userManager;
     _jwt           = jwt;
 }
        public async Task Invoke(HttpContext httpContext,
                                 IUserProvider userProvider,
                                 IJwtSecurityTokenService jwtSecurityTokenService,
                                 IUserService userService)
        {
            var rawJwt = httpContext.Request.Headers["Authorization"].FirstOrDefault();

            if (rawJwt != null)
            {
                var jwtToken = new JwtSecurityToken(rawJwt.Split(' ')[1]);

                var email = jwtToken.GetEmail();

                var user = await userService.GetUserByEmailAsync(email);

                if (jwtSecurityTokenService.ValidateRefreshToken(jwtToken, user))
                {
                    var tokenExpired             = jwtToken.ValidTo < DateTime.UtcNow;
                    var authorizationRoleChanged = !jwtSecurityTokenService.ValidateAuthorizationRole(jwtToken, user);
                    if (tokenExpired || authorizationRoleChanged)
                    {
                        var jsonString = JsonConvert.SerializeObject(new { jwtToken = jwtSecurityTokenService.RegenerateToken(jwtToken, user) });

                        httpContext.Response.ContentType = "application/json";
                        httpContext.Response.Headers.Add("Token-Expired", "true");
                        httpContext.Response.StatusCode = Constants.StatusCodes.NewAccessTokenCreated;

                        await httpContext.Response.WriteAsync(jsonString);

                        return;
                    }

                    userProvider.SetCurrentUser(jwtToken);
                }
                else
                {
                    httpContext.Response.StatusCode = Constants.StatusCodes.RedirectToLoginPage;
                    await httpContext.Response.WriteAsync(string.Empty);

                    return;
                }
            }

            await next(httpContext);
        }
Beispiel #5
0
 public LoginCommandHandler(
     IMapper mapper,
     IStringLocalizer <IdentityLocalizer> localizer,
     IHttpContextAccessor httpContextAccessor,
     IJwtSecurityTokenService jwtSecurityTokenService,
     SignInManager <ApplicationUser> signInManager,
     UserManager <ApplicationUser> userManager,
     IValidationFailureService validationFailureService,
     ILogger <LoginCommandHandler> logger)
 {
     _mapper                   = mapper;
     _localizer                = localizer;
     _httpContextAccessor      = httpContextAccessor;
     _jwtSecurityTokenService  = jwtSecurityTokenService;
     _signInManager            = signInManager;
     _userManager              = userManager;
     _validationFailureService = validationFailureService;
     _logger                   = logger;
 }
Beispiel #6
0
        public async Task InvokeAsync(HttpContext context, IJwtSecurityTokenService iJwtSecurityTokenService)
        {
            var grantResourceOwnerCredentialsContext = GrantResourceOwnerCredentialsContext.Create(context);

            if (grantResourceOwnerCredentialsContext != null)
            {
                await jwtServerOptions.AuthorizationServerProvider.GrantClientCredentialsAsync(grantResourceOwnerCredentialsContext);

                if (grantResourceOwnerCredentialsContext.IsValidated)
                {
                    var token = iJwtSecurityTokenService.Create(grantResourceOwnerCredentialsContext, jwtServerOptions);
                    await WriteResponseAsync(context, JsonConvert.SerializeObject(token));
                }
                else
                {
                    await WriteResponseError(context, grantResourceOwnerCredentialsContext.Error);
                }
            }
        }
        public async Task InvokeAsync(HttpContext context, IJwtSecurityTokenService iJwtSecurityTokenService)
        {
            var baseValidatingContext = default(BaseValidatingContext);
            var grantType             = this.GetGrantType(context);

            switch (grantType)
            {
            case Parameters.Password:
                baseValidatingContext = GrantResourceOwnerCredentialsContext.Create(context);
                if (baseValidatingContext != null)
                {
                    await jwtServerOptions.AuthorizationServerProvider.GrantClientCredentialsAsync
                        ((GrantResourceOwnerCredentialsContext)baseValidatingContext);
                }
                break;

            case Parameters.RefreshToken:
                baseValidatingContext = GrantRefreshTokenContext.Create(context);
                if (baseValidatingContext != null)
                {
                    await jwtServerOptions.AuthorizationServerProvider.GrantRefreshTokenAsync
                        ((GrantRefreshTokenContext)baseValidatingContext);
                }
                break;

            default:
                break;
            }

            if (baseValidatingContext != null)
            {
                if (baseValidatingContext.IsValidated)
                {
                    var token = await iJwtSecurityTokenService.CreateAsync(baseValidatingContext, jwtServerOptions);
                    await WriteResponseAsync(context, JsonConvert.SerializeObject(token));
                }
                else
                {
                    await WriteResponseError(context, baseValidatingContext.Error);
                }
            }
        }