コード例 #1
0
        public Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            try
            {
                //var actionLogManager = actionContext.Request.GetDependencyScope()
                //    .GetService(typeof(Log.Base.IActionLogManager)) as Log.Base.IActionLogManager;

                var serviceUrl = actionContext.Request.RequestUri.AbsolutePath;

                IAspect aspect;
                var     result = AuthorizeManager.AuthorizeMasterDataKeyValueUrl(serviceUrl, ActionKey.RequestService, out aspect);

                if (aspect.EnableLog)
                {
                    _actionLogManager?.LogOdataService(aspect.Name, actionContext.Request);
                }
                if (result)
                {
                    return(Task.FromResult(0));
                }
                actionContext.Response = new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.Unauthorized
                };
            }
            catch (System.Exception ex)
            {
                _errorLogManager.LogException(new ExceptionLog()
                {
                    Detail  = ex.ToString(),
                    Message = ex.Message,
                    Source  = ex.GetType().FullName
                });
                actionContext.Response = new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.Unauthorized
                };
            }
            return(Task.FromResult(0));
        }
        public Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            try
            {
                var requestedService = actionContext.Request.RequestUri.AbsolutePath;
                var routeTemplate    = actionContext.RequestContext.RouteData.Route.RouteTemplate;
                if (actionContext.RequestContext.RouteData.Values.Count > 0)
                {
                    var requestedServiceNormalUrl = requestedService[0] == '/'
                        ? requestedService.Substring(1)
                        : requestedService;
                    var lastCharIndex = requestedServiceNormalUrl.Length - 1;
                    requestedServiceNormalUrl = requestedServiceNormalUrl[lastCharIndex] == '/'
                        ? requestedServiceNormalUrl.Substring(0, lastCharIndex - 1)
                        : requestedServiceNormalUrl;

                    var routeTemplateNormalUrl = routeTemplate[0] == '/'
                        ? routeTemplate.Substring(1)
                        : routeTemplate;
                    lastCharIndex          = routeTemplateNormalUrl.Length - 1;
                    routeTemplateNormalUrl = routeTemplateNormalUrl[lastCharIndex] == '/'
                        ? routeTemplateNormalUrl.Substring(0, lastCharIndex - 1)
                        : routeTemplateNormalUrl;

                    var requestedServiceToken = requestedServiceNormalUrl.ToLower().Split('/');
                    var routeTemplateToken    = routeTemplateNormalUrl.ToLower().Split('/');
                    var tokenIndex            = 0;
                    requestedService = "";

                    foreach (var token in routeTemplateToken)
                    {
                        if (token == requestedServiceToken[tokenIndex])
                        {
                            requestedService += "/" + token;
                        }
                        else
                        {
                            requestedService += "/@" + actionContext.RequestContext.RouteData.Values
                                                .Where(rv => token.Contains(rv.Key.ToLower()))
                                                .OrderByDescending(rv => rv.Key.Length)
                                                .FirstOrDefault().Key;
                        }

                        tokenIndex++;
                    }
                }


                //var actionLogManager = actionContext.Request.GetDependencyScope()
                //    .GetService(typeof(Log.Base.IActionLogManager)) as Log.Base.IActionLogManager;


                IAspect aspect;
                var     result = AuthorizeManager.AuthorizeMasterDataKeyValueUrl(requestedService, ActionKey.RequestService, out aspect);
                if (aspect.EnableLog)
                {
                    _actionLogManager?.LogHttpService(aspect.Name, actionContext.Request, requestedService);
                }
                if (result)
                {
                    return(Task.FromResult(0));
                }
                actionContext.Response = new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.Unauthorized
                };
            }
            catch (System.Exception ex)
            {
                _errorLogManager.LogException(new ExceptionLog()
                {
                    Detail  = ex.ToString(),
                    Message = ex.Message,
                    Source  = ex.GetType().FullName
                });
                actionContext.Response = new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.Unauthorized
                };
            }
            return(Task.FromResult(0));
        }