草庐IT

AuthorizeAttribute

全部标签

c# - MVC 5 : Custom AuthorizeAttribute and Caching

我试图通过从中派生并覆盖其某些方法来找到实现自定义System.Web.Mvc.AuthorizeAttribute的解决方案。我正在尝试的每一种方法,我都面临着MVC5的默认授权机制中的某些问题,这使我无法正确扩展它。我已经在SO和许多专用资源上对该领域进行了大量研究,但是我无法像我目前的场景那样为这种场景找到可靠的解决方案。第一个限制:我的授权逻辑需要额外的数据,例如Controller和方法名称以及应用于它们的属性而不是HttpContextBase能够提供的有限部分数据。例子:publicoverridevoidOnAuthorization(AuthorizationCont

c# - 如何在 Web API 授权属性中获取请求 cookie?

在.NET中有两个AuthorizeAttribute类。一个在System.Web.Http命名空间中定义:namespaceSystem.Web.Http{//Summary://Specifiestheauthorizationfilterthatverifiestherequest'sSystem.Security.Principal.IPrincipal.[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,Inherited=true,AllowMultiple=true)]publicclassAut

c# - 在自定义 AuthorizeAttribute 中访问 QueryString

我正在使用WebAPI并设置了一个简单的身份验证和授权机制,调用者在该机制中传递我在查询字符串中颁发给他们的token。所以他们提交了一个请求:https://mysite.com/api/Ping?token=[issued-token]我有一个像这样的ApiAuthorizeAttribute:publicclassApiAuthorizeAttribute:System.Web.Http.AuthorizeAttribute{publicApiPermissionPermission{get;set;}publicoverridevoidOnAuthorization(Syste

c# - asp.net MVC5 - 依赖注入(inject)和 AuthorizeAttribute

我搜索了很长时间来解决我的问题。我有一个自定义AuthorizeAttribute,它需要对有权访问DbContext的“服务”具有依赖性。遗憾的是,依赖注入(inject)在自定义AuthorizeAttribute中不起作用,并且始终为null。我想出了一个(对我而言)可以接受的解决方案。现在我想知道我的解决方案是否会导致不可预见的行为?Global.asax.csCustomAuthorizeAttribute.AuthorizeServiceFactory=()=>unityContainer.Resolve();CustomAuthorizeAttribute.cs[Attr

c# - ASP.NET MVC : Problem setting the Authorize attribute Role from a variable, 需要常量

我在设置变量的授权属性角色值时遇到问题。错误消息说它需要一个const变量。当我创建一个const类型变量时,它工作正常,但我试图从Web.Config文件或任何其他允许最终用户设置它的文件加载值。我正在使用集成的Windows身份验证,因为这是一个仅限Intranet的应用程序。有没有办法从Controller检查用户角色?我将在if语句中使用它而不是属性来进行身份验证。[Authorize(Roles=Config.GMPUser)]publicActionResultIndex(){returnView();} 最佳答案 我有

c# - 您能否从 AuthorizeAttribute 返回 HTTP 响应而不抛出异常?

我在各种Controller上使用AuthorizeAttribute,这些Controller可能需要根据请求本身的某些属性返回403或429(请求太多)。我完全在自定义OnAuthorization实现中实现了它,然后在必要时抛出一个带有适当响应代码的新HttpResponseException。在我的机器上运行良好...在大规模(每分钟数千个请求)下,此实现非常糟糕,以至于它导致网站崩溃。将相同的逻辑移动到Controller操作本身并仅返回适当的HttpResponseMessage就性能而言效果很好,因此在OnAuthorization中抛出异常的代价似乎是性能问题的根本原因

c# - 使用自定义消息的 MVC 3 AuthorizeAttribute 重定向

我如何创建自定义AuthorizeAttribute以字符串参数的形式指定消息,然后将其传递到登录页面?例如,理想情况下这样做会很酷:[Authorize(Message="Accesstotheblahblahfunctionrequireslogin.Pleaseloginorcreateanaccount")]publicActionResultSomeAction(){returnView();}然后,在登录操作中,我可以这样做:publicActionResultLogin(stringmessage=""){ViewData.Message=message;returnVi

c# - 如何有效地测试 Action 是否装饰有属性(AuthorizeAttribute)?

我正在使用MVC,在我的OnActionExecuting()中,我需要确定即将执行的Action方法是否装饰有属性AuthorizeAttribute特别是。我不是在询问授权是否成功/失败,而是在询问该方法是否需要授权。对于非mvc人员filterContext.ActionDescriptor.ActionName是我正在寻找的方法名称。但是,它不是当前正在执行的方法;相反,它是一个将很快执行的方法。目前我有一个如下所示的代码块,但我对每个Action之前的循环不太满意。有更好的方法吗?System.Reflection.MethodInfo[]actionMethodInfo=t

c# - 在 ASP.NET Core MVC API Controller 上对 AuthorizeAttribute 进行单元测试

我有一个ASP.NETCoreMVCAPI,其中包含需要进行单元测试的Controller。Controller:usingMicrosoft.AspNetCore.Authorization;usingMicrosoft.AspNetCore.Mvc;usingSystem.Threading.Tasks;namespaceTransitApi.Api.Controllers{[Route("api/foo")]publicclassFooController:Controller{privateIFooRepositoryFooRepository{get;}publicFooCo

c# - 如何在 ASP.NET Core 中创建自定义 AuthorizeAttribute?

我正在尝试在ASP.NETCore中创建自定义授权属性。在以前的版本中,可以覆盖boolAuthorizeCore(HttpContextBasehttpContext)。但这不再存在于AuthorizeAttribute.目前制作自定义AuthorizeAttribute的方法是什么?我想要完成的事情:我在header授权中收到一个sessionID。根据该ID,我将知道特定操作是否有效。 最佳答案 ASP.NetCore团队推荐的方法是使用完整记录的新策略设计here.新方法背后的基本思想是使用新的[Authorize]属性来指
12