У меня есть ASP. NET MVC 4.5.2 веб-сайт (из-за ограничений хостинга), где есть область администратора с ссылкой для действий, которая загружает файл .csv или .xlsx для пользователя. при щелчке.
Локально это работает абсолютно нормально.
Однако при выходе в рабочий режим нажатие кнопки загрузки приводит к тому, что пользователь выходит из системы и отображается на экране входа в систему, как если бы он ушел. не был аутентифицирован. Затем, вход в систему приводит к тому, что загрузка фактически начинается (из-за returnUrl, установленной для ссылки действия кнопки загрузки).
Следующие фрагменты кода являются урезанными версиями того, что у меня есть:
AdminController
[Authorize(Roles = "Admin")]
public class AdminController : BaseController
{
...
[HttpGet]
public ActionResult Users()
{
var users = _context.PreSignups
.Where(x => x.IsDeleted == false)
.OrderBy(x => x.DateCreated).ToList();
CheckMessages();
return View(users);
}
[HttpGet]
public ActionResult Download_PreSignupUsersToExcel()
{
var users = _context.PreSignups
.Where(x => x.IsDeleted == false)
.OrderBy(x => x.DateCreated).ToList().ToDataTable();
DocumentService.WriteToSpreadsheet(users, $"PreSignupUsers_{DateTime.UtcNow.ToString("yyyy_MM_dd_hh_mm_ss")}.xlsx", true, Response);
// NOTE: Due to the Response being sent back in DocumentService.WriteToSpreadsheet() the below is mostly redundant as the response has already closed by this point.
TempData["Message"] = "Download started";
return RedirectToAction("Logs", "Admin");
}
DocumentService.cs
public static void WriteToSpreadsheet(DataTable dt, string filename, bool hasHeaders, HttpResponseBase response, enFileType fileType = enFileType.Xlsx)
{
... // Build the data
// Write the data
using (var exportData = new MemoryStream())
{
response.Clear();
workbook.Write(exportData);
if (fileType == enFileType.Xlsx) //xlsx file format
{
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.AddHeader("Content-Disposition", $"attachment;filename={filename}");
response.BinaryWrite(exportData.ToArray());
}
else if (fileType == enFileType.Xls) //xls file format
{
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", $"attachment;filename={filename}");
response.BinaryWrite(exportData.GetBuffer());
}
response.End();
}
}
Представление Содержит следующую ссылку действия в некоторый момент сделать GET вызов выше. Нажатие этого приводит к тому, что пользователь, по-видимому, выходит на экран входа в систему с "admin/Download_PreSignupUsersToExcel"
в качестве returnUrl.
@Html.ActionLink("Export .Xlsx", "Download_PreSignupUsersToExcel", "Admin", null, new { @class="btn btn-primary" })
Для справки, BaseController наследуется от класса Controller и предоставляет удобный способ хранения ApplicationDbContext. и LoggingService, и он не должен влиять на вышесказанное.
Я потерял код.
Есть идеи?