다음 사용자 정의 권한 부여 특성을 구현하십시오.
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public CustomAuthorizeAttribute (params string[] roleKeys)
{
var roles = new List<string>();
var allRoles = (NameValueCollection)ConfigurationManager.GetSection("CustomRoles");
foreach(var roleKey in roleKeys) {
roles.AddRange(allRoles[roleKey].Split(new []{','}));
}
Roles = string.Join(",", roles);
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Result = new RedirectResult("~/Error/AcessDenied");
}
}
}
그런 다음 (예를 들어
<CustomRoles>
<add key="UsersPagePermission" value="HR,Accounts,Developers" />
</CustomRoles>
당신의 컨트롤러 나 액션이나 글로벌 필터 같이
<section name="CustomRoles" type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
다음의 web.config에 다음을 추가 중을 선호 :)) 속성을 추가하십시오
예 :
[CustomAuthorize("UsersPagePermission")]
public class UserController : Controller
이렇게하면 권한을 변경하는 코드가 아닌 web.config를 수정할 수 있습니다.
맞춤형 Http Unauthorized 리디렉션은 선택 사항입니다 .-) – cheesesharp
whoot! 나를 도왔다! 감사! –
멋진 작품! 고마워요 :) –