1

I need to access UserId from all my asp.net mvc application in order to use it for hiding/show some elements:

the UserId the Primary Key in user tables (in SQL server).

in normal asp.net I have used a BasePage:Page and added:

public long FinKaynUserId
{
   //long FinKaynUserId = 0;
   get
   {
       if (HttpContext.Current.Session["FinKaynUserId"] != null && Convert.ToInt64(HttpContext.Current.Session["FinKaynUserId"]) != 0)
           return Convert.ToInt64(HttpContext.Current.Session["FinKaynUserId"]);
       else
       {
           HttpCookie myCookie = HttpContext.Current.Request.Cookies["FinKaynUserId"];
           if (myCookie != null)
           {
               HttpContext.Current.Session["FinKaynUserId"] = Convert.ToInt64(myCookie.Value);
               // Session["User"] = (new UserManager()).GetUser(Convert.ToInt64(Session["UserId"]));
               return  Convert.ToInt64(HttpContext.Current.Session["FinKaynUserId"]);
           }
           else
              return 0;
       }
   }
   set
   {
       HttpCookie cookie = new HttpCookie("FinKaynUserId");
       cookie.Value = value.ToString();
       cookie.Secure = false;
       cookie.Expires = DateTime.Now.AddDays(3);
       HttpContext.Current.Request.Cookies.Add(cookie);
       HttpContext.Current.Session["FinKaynUserId"] = value;
   }

}

How can i do the same thing in asp.net mvc.

8
  • i want to share a UserId between views .if the session is expired ,check cookies. Commented Jan 19, 2012 at 17:05
  • Here's another SO relevant post with MVC specific guidelines: stackoverflow.com/questions/5286301/… Commented Jan 19, 2012 at 17:16
  • You can still use the Session variables in asp.net MVC applications, but i suggest use built-in asp.net FormsAuthentication instead of managing sessions and cookies by yourself Commented Jan 19, 2012 at 19:16
  • Thanks sos.FormsAuthentication store username in a cookie.UserName is not unique in my users table (in sql server).i have to work with UserId or Email.users are authenticated with (Email,Pass)==>Get UserId==>use in all views.Is that possible with FormsAuthentication? Commented Jan 19, 2012 at 20:00
  • You don't have to use the real username as the FormsAuthentication tickets, you can tell FormsAuthentication to store anything you like such as user's emailaddress or userid and then retrieve stored ticket (key) using User.Identity.Name Commented Jan 19, 2012 at 20:11

3 Answers 3

1

HttpContext.Current.Session["Session_User"] = value;

Sign up to request clarification or add additional context in comments.

Comments

1

If the variable usage is at session level then use: Session["Variable"]="value"; Or else if usage is across the application, then use: System.Web.HttpContext.Current.Application["Variable"]="value";

Comments

-2

One option is to store your variable in the application context:

Application["FinKaynUserId"] = value;

And then you can just get it in other code section like this:

if (Application["FinKaynUserId"] != null)
{
    long FinKaynUserId = (long)Application["FinKaynUserId"];
}

3 Comments

Application variables are global variables which are shared throughout all requests, so it's not a good place to store user id. Use Session variables instead
It's a session variable.not an application variable.
@user594166 my comment was on Stas answer and usage of Application["FinKaynUserId"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.