Using Page.User.Identity.Name in MVC3

后端 未结 3 1931
清歌不尽
清歌不尽 2021-01-04 00:47

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

How

相关标签:
3条回答
  • 2021-01-04 01:15

    MVC 2

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

    MVC 3

    @this.User.Identity.Name
    
    0 讨论(0)
  • 2021-01-04 01:26

    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.

    0 讨论(0)
  • 2021-01-04 01:32

    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
    
    0 讨论(0)
提交回复
热议问题