일부 사용자 지정 검사가 내 응용 프로그램에서 액세스를 결정할 수 있도록 사용자 지정 권한 부여 특성을 만들었습니다.특정 동작에 대한 오버라이드/결합이 아닌 사용자 지정 권한 특성
컨트롤러 수준에서 사용자 지정 auth 특성을 적용한 다음 특정 동작에 추가 액세스를 추가하려고하면 역할이 '추가'방식으로 적용되지 않습니다.
은 최고 관리자 및 관리자에 대한 정식 검사를 타격 한 다음 그 자체 컨설턴트에 대한 점검을 치는 것 같다 : 컨트롤러
// Allow multiple = true so should roll all occurrences in a request into one
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class CustomAuthoriseAttribute : AuthorizeAttribute
{
public CustomAuthoriseAttribute(params string[] roles)
{
this.Roles = string.Join(",", roles);
}
/// <summary>
/// Custom routines to determine if a request is considered authorised.
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
var userManager = httpContext.GetOwinContext().GetUserManager<UserManager>();
var user = userManager.FindById(httpContext.User.Identity.GetUserId());
if (user == null)
{
return false;
}
// Log the user out as they should not be allowed access
if (user.IsDisabled || user.IsDeleted)
{
httpContext.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
httpContext.Session.Clear();
return false;
}
return base.AuthorizeCore(httpContext);
}
}
사용법 :
사용자 정의 속성을 부여 승인되지 않은 요청이 발생합니다. 왜 그들은 다 대우받지 못하고 있습니까?
코드를 디버그하고 사용자 지정 특성에서 false를 반환하는 위치를 확인 했습니까? –
'base.AuthorizeCore (httpContext) '를 호출 할 때 false로 반환됩니다. 즉, Roles 속성의 항목에 대해'user.IsInRole' 검사를 수행 할 때 실패해야 함을 의미합니다.이 경우에는 all 지정된 롤. – Tomuke
기본 메소드를 호출하기 전에 코드에서'user.IsInRole'을 검사 해 보았습니까? –