Ejemplo n.º 1
0
        public bool IsUserAuthorized(HttpActionContext actionContext)
        {
            var authHeader = FetchFromHeader(actionContext); //fetch authorization token from header


            if (authHeader != null)
            {
                var auth = new AuthenticationModule();
                JwtSecurityToken userPayloadToken = auth.GenerateUserClaimFromJWT(authHeader);

                if (userPayloadToken != null)
                {
                    var      identity         = auth.PopulateUserIdentity(userPayloadToken);
                    string[] roles            = { "All" };
                    var      genericPrincipal = new GenericPrincipal(identity, roles);
                    Thread.CurrentPrincipal = genericPrincipal;
                    var authenticationIdentity = Thread.CurrentPrincipal.Identity as JWTAuthenticationIdentity;
                    if (authenticationIdentity != null && !String.IsNullOrEmpty(authenticationIdentity.UserName))
                    {
                        authenticationIdentity.UserId   = identity.UserId;
                        authenticationIdentity.UserName = identity.UserName;
                    }
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        public JWTAuthenticationIdentity GetUserIdentity(HttpActionContext filterContext)
        {
            var authHeader = FetchFromHeader(filterContext); //fetch authorization token from header


            if (authHeader != null)
            {
                var auth = new AuthenticationModule();
                JwtSecurityToken userPayloadToken = auth.GenerateUserClaimFromJWT(authHeader);

                if (userPayloadToken != null)
                {
                    var identity = auth.PopulateUserIdentity(userPayloadToken);
                    return(identity);
                }
            }
            return(null);
        }