-1

I have an ASP.NET MVC 6 application with a few class libraries (.NET 4.6.1). Now I want to pass the values between the asp.net application and the class libraries. For example I want to access UserId (that is inside a session) from the class library. I don't want to use parameters to pass the value, because UserId is a global variable in my class library and I don't have a reference from web application in the class library. What is the best way to solve this?

  • Use Sessions in a class library?
  • Use Shared Memory ?
  • Use Web Service ?
  • Use Dtabase ?
  • ... ?

Update : https://stackoverflow.com/a/2040623/2455393 says that we can use this :

using System.Web;
var currentSession = HttpContext.Current.Session;
var myValue = currentSession["myKey"];

in .NET 4.6.1 (MVC 6) it does not work. but in .NET 4.0 it works well. this is my problem.

Community
  • 1
  • 1
Ali Borjian
  • 941
  • 10
  • 18
  • did you try http://stackoverflow.com/questions/2040607/session-in-class-how-can-i-access or http://stackoverflow.com/questions/11930829/access-session-in-class-library? – techspider May 04 '16 at 16:10
  • `"I don't have a reference from web application in the class library"` - Then how is the application referencing that code in the first place? It's not really clear to me what you're trying to do here. – David May 04 '16 at 16:12
  • This sounds to me like good use for ClaimsIdentity. – nurdyguy May 04 '16 at 16:16
  • It's clear. The class library is not depends on the web application. But the web application used the class library. In .NET 4.0 I use HttpContext.Request.Session for accessing the shared data between two. – Ali Borjian May 04 '16 at 17:13

1 Answers1

1

I don't have a reference from web application in the class library. What is the best way to solve this?

Ideally, class library should never have access to HttpContext (unless it is related to presentation layer). Instead, you just pass UserId as a parameter to methods.

Otherwise, it will be hard to unit test the class library.

How about Presentation Layer

If you want to access userId inside controller, you want to inject it, instead of accessing it from HttpContext directly.

For example,

public interface IUserSession
{
    int Id { get; }
    string FirstName { get; }
    string LastName { get; }
    string UserName { get; }
    bool IsInRole(string roleName);
}

public interface IWebUserSession : IUserSession
{
    Uri RequestUri { get; }
    string HttpRequestMethod { get; }
}

public class UserSession : IWebUserSession
{
    public int Id => Convert.ToInt32(((ClaimsPrincipal) HttpContext.Current.User)?.FindFirst(ClaimTypes.Sid)?.Value);

    public string FirstName => ((ClaimsPrincipal)HttpContext.Current.User)?.FindFirst(ClaimTypes.GivenName)?.Value;

    public string LastName => ((ClaimsPrincipal) HttpContext.Current.User)?.FindFirst(ClaimTypes.Surname)?.Value;

    public string UserName => ((ClaimsPrincipal)HttpContext.Current.User)?.FindFirst(ClaimTypes.Name)?.Value;

    public bool IsInRole(string roleName) => HttpContext.Current.User.IsInRole(roleName);

    public Uri RequestUri => HttpContext.Current.Request.Url;

    public string HttpRequestMethod => HttpContext.Current.Request.HttpMethod;
}

Usage

public class MyController : Controller
{
   private readonly IWebUserSession _webUserSession;

   public MyController(IWebUserSession webUserSession)
   {
      _webUserSession = webUserSession;
   }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • It is using (ClaimsPrincipal) HttpContext.Current.User inside the class library. The problem is that HttpContext is null in the class library (in MVC 6) – Ali Borjian May 04 '16 at 17:22
  • 1
    As I said, using **HttpContext** inside class library is wrong. You want to leave it at **Presentation Layer**. – Win May 04 '16 at 17:39
  • "As I said, using HttpContext inside class library is wrong" so what is the solution? – Ali Borjian May 04 '16 at 17:42
  • 1
    **you just pass UserId as a parameter to class library's methods** – Win May 04 '16 at 17:44
  • in a web app we use sessions because we need a straight data over the app. consider we can use parameters instead. for the same reason I need to access the sessions over the class library. – Ali Borjian May 04 '16 at 18:10
  • 1
    Without seeing your code, I could not recommend what you should do. If you follow [SOLID design principle](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)), class library should not even know what front-end you are using. Ideally, you should be able to switch from Web Application to WPF without modifying a single line of code in class library. – Win May 04 '16 at 18:20
  • I use the library just for the web app. problem is clear, check if u can use httpcontext ( session) in a class library- .net 4.5. but it is safe and sound in. net 4.0 – Ali Borjian May 04 '16 at 18:36