I'm using MVC. I need to get the current AD username (probably using SAMAccountName) and pass the value on a mailto javascript (should pass value on an email body).
Where should I put the SAMAccountName code and how will I use the value?
Technically you should be putting this into the ViewData. If you have to do this all over the place, I'd recommend subclassing Controller and overriding OnActionExecuted to get the current user's userid and putting it into the viewstate:
public class BaseController : Controller
{
protected override void OnActionExecuted( ActionExecutedContext filterContext )
{
ViewData["userid"] = Request.User.Identity;
}
}
Then have your controller extent BaseController
public class MyController : BaseController
{
public ActionResult View( int id )
{
return View();
}
}
Then in your view you can access the ViewData["userid"]
<a href="mailto:<%=ViewData["userid"]%>@mycompany.com">Email User</a>
All this being said, if you're going to be doing access control and the like, you might build a "LoggedInUser" class that you can add helper methods to. Then stick it in the session (assuming you're ok using session). You can do this logic in the Global.asax Session_Start method.