JasonH
JasonH

Reputation: 1241

MVC Display Authenticated User Information on Authenticated pages

I have an ASPX MVC 4 C# web application that has user forms authentication. Once the user logs in, I would like to display a Welcome [user] message on any authenticated page. 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.

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. 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

Upvotes: 0

Views: 4006

Answers (4)

ahmednawazbutt
ahmednawazbutt

Reputation: 833

Well you can use Cookies instead, they work same as session just they do not overload the server side. you can make them go expire when desired as well

Upvotes: 0

JasonH
JasonH

Reputation: 1241

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. 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

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.

Upvotes: 0

CSharper
CSharper

Reputation: 5580

You can store the information in Session, screw ViewBag. You can set the Session Propert in the Global.asax file. You should see and OnSessionStart method inside the Global.asax. 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. 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.

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

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

Then in your view it would be

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

Upvotes: 1

rk2531
rk2531

Reputation: 7

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.

Upvotes: 1

Related Questions