简体   繁体   English

MVC在经过身份验证的页面上显示已验证的用户信息

[英]MVC Display Authenticated User Information on Authenticated pages

I have an ASPX MVC 4 C# web application that has user forms authentication. 我有一个ASPX MVC 4 C#Web应用程序,具有用户表单身份验证。 Once the user logs in, I would like to display a Welcome [user] message on any authenticated page. 用户登录后,我想在任何经过​​身份验证的页面上显示Welcome [user]消息。 I know that User.Identity.Name pulls the user name, but I want to grab info from an additional field which would require a custom query. 我知道User.Identity.Name提取用户名,但我想从其他字段中获取信息,这需要自定义查询。

How do I go about displaying the Welcome message on all pages? 如何在所有页面上显示欢迎消息?

I made an attempt at using a Partial file, but that got me no where. 我试图使用Partial文件,但这让我无处可去。 This should be one of the easier things... to variably pass a string onto every page based off logged in user, but from my searching it is not. 这应该是更容易的事情之一...基于登录用户可变地将字符串传递到每个页面,但是从我的搜索中它不是。

I would normally provide some of my code but I'm not sure there is really much to show, more or less I need a pointer or good example in the right direction. 我通常会提供一些代码,但我不确定是否有很多要显示的内容,或多或少我需要一个指针或正确方向的好例子。

Much appreciated 非常感激

To get additional fields on a user object you can use the following: 要获取用户对象上的其他字段,可以使用以下命令:

Membership.GetUser(UserName)

and stores the message in a viewbag which you can use on all you views. 并将消息存储在Viewbag中,您可以在所有视图中使用该视图。

You can store the information in Session, screw ViewBag. 您可以在Session中存储信息,拧紧ViewBag。 You can set the Session Propert in the Global.asax file. 您可以在Global.asax文件中设置会话属性。 You should see and OnSessionStart method inside the Global.asax. 您应该在Global.asax中看到和OnSessionStart方法。 So you can say 所以你可以说

 protected void Session_OnStart()
    { 
       //Whatever is defaulted here
       System.Web.HttpContext.Current.Session["blah"] = "Your User Name"
    }

and then in the Shared Layout folder _Layout which is the default "Master Page" if you wanna call it that. 然后在共享布局文件夹_Layout中,如果您想要调用它,则默认为“母版页”。 You can call it like this whereever you like 您可以随意调用它

@String.Format("{0}",System.Web.HttpContext.Current.Session["blah"]);

Edit: 编辑:

An easy way you can use session variables is to create a Session variable class. 您可以使用会话变量的简单方法是创建Session变量类。

namespace YourSession
{
    public static class SessionProperties
   {       
      public static UserAccount UserAccountx
    {
        get
        {
            return (UserAccount)HttpContext.Current.Session["UserAccount"];
        }
        set
        {
            HttpContext.Current.Session["UserAccount"] = value;
        }
    }
   }

}

And then in your onStart() method you can say 然后在你的onStart()方法中你可以说

 YourSession.SessionProperties.UserAccountx = "Get User Account Method or w.e";

Then in your view it would be 然后在你看来它会

@String.Format("{0}",YourSession.SessionProperties.UserAccountx);

Although C Sharper pointed out a potential solution, that at first I thought worked, I decided it wasn't the exact solution I was looking for. 虽然C Sharper指出了一个潜在的解决方案,但起初我觉得有效,我认为这不是我想要的确切解决方案。 Reason being if I logged out and then back in as a new user in the same session, the session was not updating as the session was already loaded. 原因是如果我在同一会话中以新用户身份注销然后重新登录,则会话未更新,因为会话已加载。

Here is what I did: 这是我做的:

Layout Master File 布局主文件

<% if (Request.IsAuthenticated) { %>
      <div class="welcome">Welcome, <%: Html.Action( "GetUserInfo", "Member" ) %></div>
<% } %>

GetUserInfo is an ActionResult within the Member Controller GetUserInfo是成员控制器中的ActionResult

Member Controller 会员控制员

public ActionResult GetUserInfo()
{
    string userInfo = "";

    using (EntityObject db = new EntityObject())
    {
        var account = db.table_name.FirstOrDefault(u => u.UserID == User.Identity.Name);
        userInfo = account.UserDataToDisplay;
    }

    return Content(userInfo);
}

*I did change actual item names to be more generic for description purposes. *我确实将实际项目名称更改为更通用的描述目的。

Upon doing this it worked exactly as I wanted. 这样做,它完全按照我的意愿工作。 I have one method under one controller, and one line of code on the master page that upon a user being authenticated, displays the relevant information. 我在一个控制器下有一个方法,母版页上的一行代码在用户通过身份验证后显示相关信息。

Simple and Easy. 简单易行。 Just took a while to figure it out. 花了一段时间来搞清楚。

Well you can use Cookies instead, they work same as session just they do not overload the server side. 那么你可以使用Cookies,它们与会话相同,只是它们不会使服务器端过载。 you can make them go expire when desired as well 如果需要,你可以使它们过期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM