Exemple #1
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            try
            {
                var requestScope = actionContext.Request.GetDependencyScope();

                var authenticationService = requestScope.GetService(typeof(IAuthenticationService))
                                            as IAuthenticationService;
                if (authenticationService == null)
                {
                    throw BankClientException.ThrowAutofacError("AuthenticationService is null");
                }
//                var requestParams = ((AuthenticatedRequest)actionContext.ActionArguments.First().Value);
                var token       = actionContext.Request.Headers.First(p => p.Key.ToLower() == "token").Value.First();
                var parsedToken = authenticationService.CheckToken(token);
                actionContext.Request.Properties.Add("tokenObj", parsedToken);
//                requestParams.TokenObj = parsedToken;
            }

            catch (TokenExpiredException)
            {
//                var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService;
//                if (logService == null)
//                {
//                    throw BankClientException.ThrowAutofacError("LogService is null");
//                }
//                logService.Log("Token expired", "CheckToken", LogType.Warning);
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.TokenExpired());
            }
            catch (BankClientException ex)
            {
//                var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService;
//                if (logService == null)
//                {
//                    throw BankClientException.ThrowAutofacError("LogService is null");
//                }
//                logService.Log(ex.ToString(), "CheckToken", LogType.Error);
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.Unsuccessful(ex));
            }

            catch (Exception ex)
            {
//                var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService;
//                if (logService == null)
//                {
//                    throw BankClientException.ThrowAutofacError("LogService is null");
//                }
//                logService.Log(ex.ToString(), "CheckToken", LogType.Error);
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.Unsuccessful(ex));
            }
        }
Exemple #2
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            try
            {
                var requestScope = actionContext.Request.GetDependencyScope();

                var authenticationService = requestScope.GetService(typeof(IAuthenticationService))
                                            as IAuthenticationService;
                if (authenticationService == null)
                {
                    throw BankClientException.ThrowAutofacError("AuthenticationService is null");
                }
                var token       = actionContext.Request.Headers.First(p => p.Key.ToLower() == "token").Value.First();
                var parsedToken = authenticationService.CheckToken(token);
                actionContext.Request.Properties.Add("tokenObj", parsedToken);

                if (Roles != null)
                {
                    var userManager = Startup.UserManagerFactory();
                    var tokenObj    = new ParsedTokenHelper().GetParsedToken(actionContext.Request.Properties);
                    var userId      = tokenObj.UserId;
                    if (Roles.Any(role => userManager.IsInRole(userId, role.ToString())))
                    {
                        return;
                    }
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }

            catch (TokenExpiredException)
            {
                //                var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService;
                //                if (logService == null)
                //                {
                //                    throw BankClientException.ThrowAutofacError("LogService is null");
                //                }
                //                logService.Log("Token expired", "CheckToken", LogType.Warning);
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.TokenExpired());
            }
            catch (BankClientException ex)
            {
                //                var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService;
                //                if (logService == null)
                //                {
                //                    throw BankClientException.ThrowAutofacError("LogService is null");
                //                }
                //                logService.Log(ex.ToString(), "CheckToken", LogType.Error);
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.Unsuccessful(ex));
            }

            catch (Exception ex)
            {
                //                var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService;
                //                if (logService == null)
                //                {
                //                    throw BankClientException.ThrowAutofacError("LogService is null");
                //                }
                //                logService.Log(ex.ToString(), "CheckToken", LogType.Error);
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.Unsuccessful(ex));
            }
        }