- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 ASP.NET MVC4 Web 应用程序有一个自定义主体/身份。我还创建了一个 AuthorizeAttribute 来实例化我的自定义主体,将其分配给我需要身份验证的 Controller 中的 httpContext.User。
这对于使用我的 AuthorizeAttribute 修饰的 Controller /操作非常有用但是,对于不需要身份验证的 Controller (但如果存在,仍然使用它),我想获取我的 CustomPrincipal(最好通过 HttpContext.User) .
在这些未修饰的 Controller /操作中,设置了 HttpContext.User,但使用的是 GenericPrincipal 而不是我的 CustomPrincipal。 将 HttpContext.User 的默认设置“覆盖”到 GenericPrincipal 的最佳位置在哪里?
同样,如果在每个具有身份验证 cookie 的请求中都这样做,在 AuthorizeAttribute 修饰的 Controller 的情况下,我将如何避免做两次工作(这将成为一个强制认证)。
为了清楚起见,我的网站允许匿名用户访问,但在这些页面上,如果一个经过身份验证(并且实现了 CustomPrincipal),则提供了额外的功能。
我认为一些选项是(不确定每个选项背后的逻辑):
最佳答案
您可以使用全局操作过滤器。假设您有一个自定义主体:
public class MyPrincipal : GenericPrincipal
{
public MyPrincipal(IIdentity identity, string[] roles): base(identity, roles)
{
}
... some custom properties and stuff
}
AuthorizeAttribute
派生来避免全局身份验证,它只是实现了
IAuthorizationFilter
接口(interface)以确保它在任何其他过滤器之前运行):
public class GlobalIdentityInjector : ActionFilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var identity = filterContext.HttpContext.User.Identity;
// do some stuff here and assign a custom principal:
var principal = new MyPrincipal(identity, null);
// here you can assign some custom property that every user
// (even the non-authenticated have)
// set the custom principal
filterContext.HttpContext.User = principal;
}
}
~/App_Start/FilterConfig.cs
中注册以便保证它将适用于所有操作:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new GlobalIdentityInjector());
}
}
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var authorized = base.AuthorizeCore(httpContext);
if (!authorized)
{
return false;
}
// we know that at this stage we have our custom
// principal injected by the global action filter
var myPrincipal = (MyPrincipal)httpContext.User;
// do some additional work here to enrich this custom principal
// by setting some other properties that apply only to
// authenticated users
return true;
}
}
public ActionResult Foo()
{
var user = (MyPrincipal)User;
// work with the custom properties that apply only
// to anonymous users
...
}
[MyAuthorize]
public ActionResult Bar()
{
var user = (MyPrincipal)User;
// here you can work with all the properties
// because we know that the custom authorization
// attribute set them and the global filter set the other properties
...
}
关于asp.net - 如何全局创建 CustomPrincipal(有和没有 AuthorizeAttribute),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11476301/
这是我的 CustomAuthorizeAttribute 类: public class CustomAuthorizeAttribute : AuthorizeAttribute {
TL;博士 :当 IdentityServer3 将表单发布到始终是根 url 的返回 url 时,MVC 应用程序如何知道在通过身份验证过程后重定向到某个操作? 此问题中的示例取自 Identity
在我们的 MVC 解决方案中,我们有两个 AuthorizeAttribute 的自定义实现 - 一个名为 BasicHttpAuthorizeAttribute 并已在生产中使用多年,另一个 Rol
我的应用程序中有一个自定义的 AuthorizeAttribute,它接受一个输入参数 bool UserIsOnline。此参数用于增加一个表字段,该字段包含有关上次用户交互时间的信息,即对于在后台
我有一个 ApiController 类,其中有 10 个公共(public)方法。 在这 10 个方法中,有 9 个需要 [Authorize(Roles="Admin")]。不需要的,不需要任何授
我目前正在尝试根据用户角色在新的 ASP MVC 5 应用程序中实现安全性。目标是防止用户在没有特定角色(或更高级别)的情况下访问某些 Controller 或 Controller 方法。根据到目前
我正忙着为我的名为 MyAuthorizeAttribute 的操作方法编写自己的自定义属性,我还在忙着编写代码,这是我的部分代码: [AttributeUsage(AttributeTargets.
我创建了一个自定义 AuthorizeAttribute 类来处理我的 MVC4 应用程序中的精细授权。 这是类: [AttributeUsage(AttributeTargets.Method, A
我已经从 AuthorizeAttribute 实现了 RoleAuthorize 类 public sealed class RoleAuthorize : AuthorizeAttribute {
我正在使用 MVC3/4。但这只是授权中的一般问题。 我有一个角色在数据库中名为“Trip Leader”,其中包含一个空格。 我尝试了 [Authorize(Roles="'Trip Leader'
我们想授权用户,如果他们被授权,我们想将他们的角色和权限添加到用户并将其添加到 IPrinciple 我们有两种方法可以做到这一点,一种是在 global.asax Application_Authe
我一直在互联网上寻找为什么我的自定义 AuthorizeAttribute 在我的 MVC WebApi 中不起作用。我看到有人在 SO 上问过这种事情,但还没有帮助我解决我的问题: [Attrib
我想使用标准的 AuthorizeAttribute(即不继承它)但使用自定义重定向。那可能吗?我应该在哪里检查 401 并重定向? 我试过添加 但它没有用。 最佳答案 在我的 A
在 Windows 域内网站点(使用 )上工作时,我遇到了以下问题: [Authorize(Roles = "Domain Users, Domain Admins")] public class
我有一个像这样的自定义 AuthorizeAttribute public class DevMode : AuthorizationFilterAttribute { public over
我试图阻止特定角色(比如 RoleA)中的特定用户访问特定操作。允许匿名用户访问,但不允许 RoleA 中的用户访问该操作。 所以我做了这样的事情: [AllowAnonymous] [CustomA
如果我做了more than one API调用同时出现错误 A second operation started on this context before a previous asynchro
我有一个自定义的 RequireHttpsAttribute 和一个自定义的 AuthorizeAttribute,我在 FilterConfig 中应用它们以确保所有 Controller 都使用
在我的 MVC 5 应用程序中,我按如下方式装饰我的 Controller : [Authorize] public class Controller { .. 但是,我的一个要求是使用 toke
我已经研究了一段时间,现在试图弄清楚如何在我的 View 中使用我的自定义 AuthorizeAttribute 类来显示和隐藏链接。我正在从 IsInRole 过渡到自定义 AuthorizeAtt
我是一名优秀的程序员,十分优秀!