1

I'd like to use ASP.NET MVC's views as mail template engine. For that, I am calling one controller action from another controller action using a System.ComponentModel.Component.WebClient, parse the returned web page and send it via e-mail.

In this scenario, is it possible to forward the current user's login credentials (I am using FormsAuthentication) to the controller action requested by the WebClient? User passwords are encrypted, so I can't just create a new NetworkCredentials instance with his user name and password.

Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210

2 Answers2

5

Yes, you can just copy the .ASPXAUTH cookie from your current Request object to the WebClient

EDIT: I haven't actually tried this myself, so maybe the .ASPXAUTH cookie is removed from the Request object for security reasons.

But since you have access to the machine key, you can create your own cookies on the fly. Here's the code that should do it (I can't find the project where I actually did that)

var ticket = new FormsAuthenticationTicket(User.Identity.Name, true, 5);
string aspxAuthCookieValue = FormsAuthentication.Encrypt(ticket);

This code creates a forms authentication cookie for your current user name and with an expiration time of 5 minutes.

chris166
  • 4,769
  • 4
  • 24
  • 25
1

Instead of performing a http request, aren't you looking for something like "rendering a view to a string"

Community
  • 1
  • 1
veggerby
  • 8,940
  • 2
  • 34
  • 43
  • 1
    That's what I tried first too, but after much googling around and trying different approaches for doing this (none of which were working with ASP.NET MVC 1.0 final), I chose a webclient instead. Performance is not an issue here, so I don't really care about the overhead. – Adrian Grigore Aug 07 '09 at 13:04