18

In MVC2 I have used Page.User.Identity.Name using the <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

How can I use the same in MVC3?

learning
  • 11,415
  • 35
  • 87
  • 154

3 Answers3

29

You can always do something like:

@Html.ViewContext.HttpContext.User.Identity.Name

but don't.

Normally a view shouldn't try to fetch such information. It is there to display whatever information is passed by the controller. It should be strongly typed to a model class which is passed by a controller action.

So in the controller action rendering this view:

[Authorize]
public ActionResult Index()
{
    var model = new MyViewModel
    {
        Username = User.Identity.Name
    }
    return View(model);
}

Now inside the view feel free to use this information:

@Model.Username
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have not understood, i`m using FormsAuthentication.RedirectFromLoginPage(user.Name, model.RememberMe); and in another view i`m trying to load the user name using in _LogOnPartial.cshtml using @if(Request.IsAuthenticated) { Welcome @Page.User.Identity.Name! [ @Html.ActionLink("Log Off", "LogOff", "Account") ] } else { @:[ @Html.ActionLink("Log On", "LogOn", "Account") ] } – learning Jan 06 '11 at 10:48
  • What`s the use of _LogOnPartial.cshtml in that case? and I just can`t do that for all the views? – learning Jan 06 '11 at 10:52
  • 1
    @user281180, you shouldn't use `FormsAuthentication.RedirectFromLoginPage` in an ASP.NET MVC application. The correct way of performing a redirect is to `return RedirectToAction("LoggedIn", "SomeController")` after having set the authentication cookie to the response (using [FormsAuthentication.SetAuthCookie](http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.setauthcookie.aspx)). Then inside the LoggedIn action simply fetch the username and pass it to the view model. So use `@Model.Username` instead of `@Page.User.Identity.Name`. – Darin Dimitrov Jan 06 '11 at 10:53
  • Of course if you don't want to use strongly typed views you can always use `@Html.ViewContext.HttpContext.User.Identity.Name`. – Darin Dimitrov Jan 06 '11 at 10:55
  • Can I have an example how I can use the FormsAuthentication.SetAuthCookie please. Actually, in my mvc2 I have used FormsAuthentication.RedirectFromLoginPage and since it worked, I have used it for MVC3 as well – learning Jan 06 '11 at 11:00
  • @user281180, `RedirectFromLoginPage` works but it is not correct to use it in MVC. As far as the `SetAuthCookie` is concerned simply: `FormsAuthentication.SetAuthCookie(user.Name, model.RememberMe);`. – Darin Dimitrov Jan 06 '11 at 11:31
  • One thing i`m confused about is, if I Use FormsAuthentication.SetAuthCookie(user.Name, model.RememberMe), next time anyone logging in the system, will be logged as the previous user logged, however, I only want to pass the user status to the page after the login Page. – learning Jan 06 '11 at 11:37
  • 2
    `SetAuthCookie` is absolutely identical to `RedirectFromLoginPage` except that it doesn't perform the redirect because as I explained redirects in ASP.NET MVC should be performed by a controller action returning a RedirectToAction result. – Darin Dimitrov Jan 06 '11 at 11:43
11

MVC 2

<%: this.Page.User.Identity.Name %>

MVC 3

@this.User.Identity.Name
Nikita Ignatov
  • 6,872
  • 2
  • 34
  • 34
1

I had the same problem. I used this tutorial to solve this issue.

In short, in your view, put this code:

@Context.User.Identity.Name

Just make sure that the project is set to authenticate with windows.

StarPilot
  • 2,246
  • 1
  • 16
  • 18