3

Background:

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. This works for that application but when we have a class library referenced in that application and we need Session Variables in that library, I have to pass it to each method as I am not using dependency injection.

Problem:

I have a controller which does Facebook authentication as below:

 public class FacebookController : Controller
    {
        private FacebookGateway FacebookGateway = new FacebookGateway();

        // GET: /Facebook/

        public ActionResult Index()
        {
            string code = Request.QueryString["code"];
            TokenResponse tokenResponse = FacebookGateway.GetTokenResponse(code);
            Session["AccessToken"] = tokenResponse.Access_token;
            return View();
        }

        public ActionResult Friends()
        {

            string accessToken = Session["AccessToken"].ToString();

            WebRequest request = WebRequest.Create(instanceUrl + @"profileUrl-Here");
            request.Method = "GET";
            request.Headers.Add("Some Header " + accessToken);
            WebResponse response = request.GetResponse();
            string str = String.Empty;

            if (response == null)
            {
            }
            else
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    str = sr.ReadToEnd().Trim();

            }
           //Convert str to Model
            return View(model);
        }

Here, in almost each method I need access token for data access from Facebook Graph API, but, I have to take it from session each time as follows

 var accessToken = Session["AccessToken"].ToString();

How can I make it available to all the methods in the controllers?

Is there any way to share the session between MVC and class library without passing HttpContext.Session in each method call for class library?

Note: I have seen this answer, but it was not quite clear how can it be available in class library too.

Community
  • 1
  • 1
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • Sounds like you would benefit adding a level of abstraction between your class library and MVC app. Doing so would allow you to pass the necessary information from the MVC app to the library without the library actually knowing how to read it from the Session itself. – M.Babcock Aug 31 '13 at 21:07
  • what is purpose of your library & what does it do? – Akash Kava Sep 01 '13 at 05:47
  • @AkashKava Is it really needed? – Bhushan Firake Sep 01 '13 at 05:49

1 Answers1

8

Related to this answer (the one you mentioned):

You can put the session wrapper (i.e. the MySession class) into your class library. That way it can be used by code in the class library as well as from code in the web app.

Generally, you can access the session state from everywhere by using System.Web.HttpContext.Current.Session["AccessToken"]. This works even without any wrapper class.

Community
  • 1
  • 1
M4N
  • 94,805
  • 45
  • 217
  • 260
  • How can I put the values in the session if I make this wrapper class? Should I make another method in the same to assign or store values in session? – Bhushan Firake Aug 31 '13 at 21:13
  • @BhushanFirake: just use the `MySession` class's properties. You can get and set them, e.g. `MySession.AccessToken = tokenResponse.Access_token` – M4N Aug 31 '13 at 21:16
  • So, if I have to use this class in many methods of the controller, I will have to make it private for that controller. Right? – Bhushan Firake Aug 31 '13 at 21:18
  • Just curious about your second line: "you can access the session state from everywhere". My class library has to have a reference to `System.Web`, if I want to do this or there is a workaround for that? – Bhushan Firake Aug 31 '13 at 21:21
  • @BhushanFirake : You could create a Base controller and now create protected properties to access Session Values. Finally your Controller will be derived from Base Controller. Confusion ? – Imad Alazani Sep 01 '13 at 03:18
  • @PKKG In know this can be done to access `Session` in the controller methods, but what about the class library? – Bhushan Firake Sep 01 '13 at 05:00