Я предполагаю, что человек может быть анонимным и загружать файл, если он / она предоставил детали ранее.В этом случае проигнорируйте атрибут Authorization и напишите свой собственный.Вот быстрый пример.Он полагается на файл 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, чтобы вы могли перенаправить пользователя на страницу загрузки после того, как он предоставил свой адрес электронной почты.
EDIT
Согласно 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
}
}
}