Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC 3属性_Asp.net_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

ASP.NET MVC 3属性

ASP.NET MVC 3属性,asp.net,asp.net-mvc,asp.net-mvc-3,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我正在使用ASP.NET MVC 3开发一个项目,现在使用MembershipProvider、RoleProvider和custom。因此,在代码的某些部分,请使用: [Logon(Roles = "login, test1")] 此代码非常适合在MembershipProvider代码中使用: public override string [] GetRolesForUser (string username) { var = UsuarioRepository.GetListaP

我正在使用ASP.NET MVC 3开发一个项目,现在使用MembershipProvider、RoleProvider和custom。因此,在代码的某些部分,请使用:

[Logon(Roles = "login, test1")]
此代码非常适合在MembershipProvider代码中使用:

public override string [] GetRolesForUser (string username)
{
    var = UsuarioRepository.GetListaPermissoesByUsuarioEmail permissions (username);

    if (permissions == null)
    {
        nullPermissao var = new string [0];
        nullPermissao return;
    }

    return permissions;
}
我的问题是。如何使用以下代码,需要自定义哪种方法?
我想检查是否确定某个特定类型的用户已登录,以及该用户是否具有某些权限

[Logon(Roles = "login, test1," Users = "User1")]

使用override string[]GetRolesForUser(字符串用户名)方法检查角色,我可以用哪种方法检查用户

您是否打算使用以下内容

[Authorize(Roles = "login, test1", Users = "User1")]

这应该是authorized属性的开箱即用。它检查HttpContext.User.Identity.Name是否与您在AuthorizeAttribute.Users下定义的任何术语匹配

正如我从评论中看到的,您在可能重写OnAuthorize方法的地方使用了自己的LogonAttribute。这就是授权属性发挥作用的地方,它很神奇

原始ASP.NET MVC源代码

protectedvirtualbool-AuthorizeCore(HttpContextBase-httpContext)
{
if(httpContext==null)
抛出新ArgumentNullException(“httpContext”);
IPrincipal user=httpContext.user;

return user.Identity.IsAuthenticated&&(this.\u usersSplit.Length)您是否试图将对方法的访问限制为特定用户?我不理解您的问题。您试图实现什么?抱歉,我键入的方式错误,实际上是“登录”我从AuthorizeAttribute类创建的自定义属性。我仍然不明白您在问什么。Logon属性和您显示的GetRolesForUser方法之间的关系是什么?我想检查是否确定登录的特定类型的用户以及该用户是否具有某些特权。使用覆盖字符串[]GetRolesForUser(字符串用户名)方法它检查角色,在哪种方法中我可以检查用户?不确定您要问什么,但这是要使用的正确属性,然后您可以覆盖默认角色和成员资格提供程序,以自定义方式控制用户和角色,而不是默认的aspnet数据库表。您必须向我们更新web.config不过,我也会选择其他供应商。
protected virtual bool AuthorizeCore(HttpContextBase httpContext)
{
  if (httpContext == null)
    throw new ArgumentNullException("httpContext");
  IPrincipal user = httpContext.User;
  return user.Identity.IsAuthenticated && (this._usersSplit.Length <= 0 || Enumerable.Contains<string>((IEnumerable<string>) this._usersSplit, user.Identity.Name, (IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase)) && (this._rolesSplit.Length <= 0 || Enumerable.Any<string>((IEnumerable<string>) this._rolesSplit, new Func<string, bool>(user.IsInRole)));
}

public virtual void OnAuthorization(AuthorizationContext filterContext)
{
  if (filterContext == null)
    throw new ArgumentNullException("filterContext");
  if (OutputCacheAttribute.IsChildActionCacheActive((ControllerContext) filterContext))
    throw new InvalidOperationException(MvcResources.AuthorizeAttribute_CannotUseWithinChildActionCache);
  if (this.AuthorizeCore(filterContext.HttpContext))
  {
    HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
    cache.SetProxyMaxAge(new TimeSpan(0L));
    cache.AddValidationCallback(new HttpCacheValidateHandler(this.CacheValidateHandler), (object) null);
  }
  else
    this.HandleUnauthorizedRequest(filterContext);
}