과 같은 사용자 정의 AuthorizeAttribute 만들기 : 당신을
[MyAuthorize]
public class HomeController : Controller
{
}
또는 :이처럼 컨트롤러에서 사용할 수 있습니다, 그리고
public class MyAuthorizeAttribute : AuthorizeAttribute
{
private UnitOfWork _unitOfWork = new UnitOfWork();
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = false;
var username = httpContext.User.Identity.Name;
// Some code to find the user in the database...
var user = _unitOfWork.UserRepository.Find(username);
if(user != null)
{
// Check if there are Details for the user in the database
if(user.HasDetails)
{
isAuthorized = true;
}
}
return isAuthorized;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (!AuthorizeCore(filterContext.HttpContext))
{
// If not authorized, redirect to the Details action
// of the Account controller...
var action = filterContext.RouteData.Values["action"];
if(filterContext.Controller is AccountController
&& action.Equals("Details"))
{
// Do nothing
}
else
{
filterContext.Result = new RedirectToRouteResult(
new System.Web.Routing.RouteValueDictionary {
{"controller", "Account"}, {"action", "Details"}
}
);
}
}
}
}
을 다음과 같이 Global.asax 파일에 전역 작업 필터로 등록 할 수 있습니다.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyAuthorizeAttribute());
}
데이터베이스에 저장된 사용자에 대한 세부 정보가 있는지 확인하고, 그렇지 않은 경우 사용자를 '세부 정보 입력'보기로 리디렉션하는 사용자 지정 권한 특성을 만듭니다. – ataravati
가벼운 코드 예제와 함께 해당 결과에 대한 답변을 게시 할 수 있습니까? 폼 인증 (View Auth)과 마찬가지로 권한 부여보기 (View on Authorization)로 안내 할 수있는 방법이 있기를 바랬습니다. 또한 사용자 지정 특성을 구현할 경우 windows auth에서 세부 정보를 가져 오는 방법을 알아야합니다. –