나는 그 사람이 전에 익명으로있을 수 있고 she/he가 전에 세부 사항을 제공하는 한 파일을 다운로드 할 수 있다고 가정합니다. 이 경우 Authorization 속성을 무시하고 직접 작성하십시오. 다음은 간단한 예입니다. 그것은 설정 될 쿠키에 의존합니다.
public class CheckEmailAddressAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;
// if the cookie isn't set the a user never provided their details
if (request.Cookies["mycookiename"] == null)
{
// Set it to the correct result, to redirect the user to provide their email address
filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" };
filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true"));
}
else
{
// Don't do anything, the user already provided their email address
}
}
}
다운로드 할 컨트롤러에서 이와 같이 지정하십시오.
public class DownloadController : Controller
{
[CheckEmailAddress]
public ActionResult Download(string name)
{
// Implement download
}
}
이메일 주소가 설정되면 쿠키를 설정해야합니다. 나는 당신이 그 일을하는 방법을 알고 있다고 생각합니다. "returnUrl"매개 변수가 있는지 확인하여 이메일 주소를 제공 한 사용자를 다운로드 페이지로 리디렉션 할 수도 있습니다.
편집 OP 당으로, 우리가 여기에 업데이트 된 클래스입니다, 사람이 자신의 정보를 입력하면 제외 쿠키를 설정하지 않습니다.
public class CheckEmailAddressAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;
// if the cookie isn't set the a user never provided their details
if (request.Cookies["mycookiename"] == null)
{
// Set it to the correct result, to redirect the user to provide their email address
filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" };
// filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true"));
}
else
{
// Don't do anything, the user already provided their email address
}
}
}
역할을 사용해야합니다. [http://stackoverflow.com/questions/6404254/mvc-3-dynamic-authorization-of-multiple-roles-and-users][1] [1]에 http : // 유래. com/questions/6404254/mvc-3-multiple-roles-and-users –