从母版页访问User.Identity

And*_*ewC 14 c# asp.net asp.net-mvc-2

我正在尝试从我的母版页访问User.Identity,这样我就可以确定哪个用户已登录,但是我无法让它工作.如果我System.Security.Principal在我的母版页中导入它没有区别:

<%@ Import Namespace="System.Security.Principal" %>
Run Code Online (Sandbox Code Playgroud)

如果我在Controller中尝试,我可以很好地访问它.

知道我需要做什么吗?

Mat*_*ott 54

通过HttpContext.Current.User.Identity怎么样?


tho*_*kia 8

<%=HttpContext.Current.User.Identity.Name %>将显示当前用户名将 HttpContext.Current.User获取IPrincipal对象.

这是一个仅在标题中显示用户名的母版页:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>
        <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    </title>
    <link href="../../Content/Style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1 id="maintitle">
                    <%=HttpContext.Current.User.Identity.Name %>
                </h1>
            </div>
        </div>
        <div id="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />
        </div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)