views:

180

answers:

3

Hi,

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.

A: 

I think yes. Its possible.

asb
-1: You don't say!
Adrian Grigore
Technically, he fully answered the only actual question that you asked. :)
James S
Yes, but he did not pass the turing test! :-)
Adrian Grigore
+2  A: 

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
Sounds good and I figured out the webclient part of setting a cookie, but how can I read .ASPXAUTH? For some reason the following term is true, even though I am logged in: requestContext.HttpContext.Response.Cookies[".ASPXAUTH"].Value.Length==0
Adrian Grigore
I've added a way around this in my answer
chris166
Just read your comment again: you should look in HttpContext.REQUEST not RESPONSE !
chris166
Yup you're right. Thanks for the hint and sorry for the mixup!
Adrian Grigore
+1  A: 

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

veggerby
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