我假設這個人可以是匿名的,只要她/他提供了詳細信息之前下載該文件。在這種情況下,請忽略授權屬性並編寫您自己的。這是一個簡單的例子。它依靠一個cookie來設置。
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
}
}
剩下的唯一事情就是在設置電子郵件地址時設置cookie。我假設你知道如何做到這一點。您可能還需要確保您有一些「returnUrl」參數,以便您可以在用戶提供其電子郵件地址後將用戶重定向到下載頁面。
編輯
按OP,我們不想設置Cookie,除非該人進入他們的詳細資料,所以這裏是更新後的類。
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-dynamic-authorization-of-multiple-roles-and-users – 2012-03-09 10:23:24