public void GetClaimsTest() { string jwt = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJVdWlkIjoiZTA1OGY1NDgtM2IxOC00MTg3LWI0YzItM2QxMDEyMmY4ODdjIiwiVXNlcm5hbWUiOiJUZXN0IiwiQWNjb3VudFJvbGUiOiJVc2VyIiwiYXVkIjoiaHR0cDovL3Rlc3QuZXhhbXBsZS5jb20iLCJleHAiOjE2MTg4NjQ3MzQsImlzcyI6IkF1dGgiLCJpYXQiOjE2MTg4NjM4MzQsIm5iZiI6MTYxODg2MzgzNH0.rxkUiPdZD7fk_ar2erfTLHhwjQK1CDI9kSvgPhTifOgeq0s8M2Glbp5vIa5jIsblmaI0SL1GeWD07j8dYB3bMA"; Assert.IsTrue(_jwtLogic.GetClaim <Guid>(jwt, JwtClaim.Uuid) != Guid.Empty); Assert.IsTrue(_jwtLogic.GetClaim <AccountRole>(jwt, JwtClaim.AccountRole) != AccountRole.Undefined); }
public async Task GetClaimGuidTest() { AuthorizationTokensViewmodel result = await _jwtLogic.CreateJwt(new TestUserDto().User); Guid userUuid = _jwtLogic.GetClaim <Guid>(result.Jwt, JwtClaim.Uuid); Assert.AreNotEqual(userUuid, Guid.Empty); }
public UserDto GetRequestingUser(ControllerBase controllerBase) { string authorization = controllerBase.HttpContext.Request.Headers[RequestHeaders.Authorization]; string jwt = authorization.Replace("Bearer ", ""); if (jwt.Length < 25) { throw new UnprocessableException(); } return(new UserDto { Uuid = _jwtLogic.GetClaim <Guid>(jwt, JwtClaim.Uuid), AccountRole = _jwtLogic.GetClaim <AccountRole>(jwt, JwtClaim.AccountRole) }); }
public override void OnActionExecuting(ActionExecutingContext context) { bool allowAnonymous = context.ActionDescriptor.EndpointMetadata .Any(em => em.GetType() == typeof(AllowAnonymousAttribute)); if (allowAnonymous) // skip authorization if allow anonymous attribute is used { return; } JwtLogic jwtLogic = (JwtLogic)context.HttpContext.RequestServices.GetService(typeof(JwtLogic)); string authorization = context.HttpContext.Request.Headers[RequestHeaders.Authorization]; if (string.IsNullOrEmpty(authorization)) { context.Result = new UnauthorizedResult(); base.OnActionExecuting(context); return; } string jwt = authorization.Replace("Bearer ", ""); var role = jwtLogic.GetClaim <AccountRole>(jwt, JwtClaim.AccountRole); if (!_requiredRoles.Contains(role)) { context.Result = new UnauthorizedResult(); } base.OnActionExecuting(context); }