Handling cookies in a seperate class file in C# ASP.NET MVC

I have made a Single Sign On system and I am making a class library for handling cookies. But I am not able to get the server (ASP.NET MVC Controlller) instance into the class library. Please find attached my .cs file in which I have methods that can handle all cookies in my application. Should I pass the HTTPContext object from my web application to the class library (into the method) or the Response object? I have tried both but in vain!

public void DoLogin(string username, bool autoSignIn)
{
    HttpContext.Current.Response.Cookies["nowenableLogin"]["username"] = username;
    HttpContext.Current.Response.Cookies["nowenableLogin"]["autoSignIn"] = autoSignIn.ToString();
    HttpContext.Current.Response.Cookies["nowenableLogin"]["lastVisit"] = DateTime.Now.ToString();
    HttpContext.Current.Response.Cookies["nowenableLogin"]["status"] = true.ToString();
    HttpContext.Current.Response.Cookies["nowenableLogin"].Expires = autoSignIn ? DateTime.Now.AddDays(7) : DateTime.Now.AddDays(1);
}

Answers


I have had the same problem, while trying to read a "cshtml" file. I saved it in a variable, and sent it as a body in an email. The way i solved it, was by creating a new instance of a controller inside the controller class (where the method from helper class was called giving it parameters). But in this case, you just need HttpContext, so replacing my idea of ControllerContext, in this way it would be using

This snippet in controller:

 HelperClass.DoLogin("username",true,HttpContext.Current);

And then, in the helper class you can just use:

public void DoLogin(string username, bool autoSignIn,HttpContext context)
{
     var cookie = context.Response.Cookies["cookieName"];
}

Need Your Help

Are there in jQuery some methods for handling period of time data type?

c# javascript jquery

I'm new in jQuery and all of my search requests bring me to methods for handling Date/Time data types. But I need to handle some period of time, given in number of minutes. I want to convert, for e...