My main goal is to generate an XML file that can be downloaded.
The below code succeeds in generating the file but the view (/Home/XmlView) can be accessed directly.
If I use the [Authorize] attribute on the XmlView() method then the file is created with the HTML of my default logon page.
How do get the authorization to work with the XmlView() method or do all of this more efficiently?
I am open to any any suggestions.
Thanks in advance.
using System;
using System.Net;
using System.Text;
using System.Web.Mvc;
using MvcProject.Core.Repositories;
namespace MvcProject.Core.Web.Controllers
{
[HandleError]
public class HomeController : Controller
{
private readonly IEntryRepository _entryRepository;
public HomeController(IEntryRepository entryRepository)
{
_entryRepository = entryRepository;
}
[HttpGet]
[Authorize]
public ActionResult Download()
{
var url = Url.Action("XmlView", "Home", null, "http");
var webClient = new WebClient
{
Encoding = Encoding.UTF8,
UseDefaultCredentials = true
//Credentials = CredentialCache.DefaultCredentials
};
var fileStringContent = webClient.DownloadString(url);
var fileByteContent = Response.ContentEncoding.GetBytes(fileStringContent);
return File(fileByteContent, "application/xml",
string.Format("newfile{0}.xml", DateTime.Now.ToFileTime()));
}
//[Authorize]
public ActionResult XmlView()
{
return View("XmlView", _entryRepository.GetAll());
}
}
}