0

I have been trying to get my head around OWIN and Identity to get it play well with my Existing app. The application was coded in Plain ASP.Net and I want to port it to MVC to organize it better. The app have established database including Users table so using Identity table is not option. The wall I hit with Identity is that it requires Email and my system is NOT supposed to demand user email. After trying almost everything (days reading almost every answer on the subject) I have come to conclusion that it will take more time to get it done, and that brings me to my question.

Being PHP guy (jumped to ASP.Net not long ago) I could easily make a working login in 10minutes and now I need a thing like it, something like

$user = User::findByUserName($username); //somehow here I find user from database
if($user!=null)
{
    $_SESSION['isLoggedIn'] = true;
    $_SESSION['fullname'] = $user->fullName;
}
else
{
    //redirect to login with error
}

I cannot find a session class in MVC5. Is that even possible? If no, is there a simple route to solve my issue?

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93

2 Answers2

1

The most common way to cache is to either place it in the 'Global' server cache or place it in the session. There are other ways to save state and I would recommend looking into alternatives as the techniques below use the System.Web namespace. System.Web will not be included in MVC6.

Global Cache

//Save User
System.Web.HttpContext.Current.Cache.Insert(
                       "CurrentUser", 
                        User, 
                        null, 
                        System.Web.Caching.Cache.NoAbsoluteExpiration, 
                        new TimeSpan(0,20,0), 
                        System.Web.Caching.CacheItemPriority.Normal, 
                        null);       
//Get User   
User user=(User)System.Web.HttpContext.Current.Cache["CurrentUser"];

Session Cache

//Save User   
HttpContext.Current.Session.Add("CurrentUser",User); 

//Get User
User=(User)HttpContext.Current.Session["CurrentUser"];
Community
  • 1
  • 1
Ross Bush
  • 14,648
  • 2
  • 32
  • 55
  • is there a way to associate it with [Authorize] so that I don't have to check in each and every controller by myself? – Stefano Mtangoo Jun 28 '15 at 17:42
  • Can't you put the ticket in a cookie. That way it will be on each request. Put the identity in the cookie and read it as needed. This is built into forms authentication. When cookies are supported. Doesn't the OWIN wrappers have built in support for this? – Ross Bush Jun 28 '15 at 21:48
  • Also, you can place the check code in an overriden Controller.OnAuthorization(), it is called automatically. – Ross Bush Jun 28 '15 at 21:51
1

you could basically tell owin manuelly to authenticate the user without using the identity-stuff (i dont like it either, at least for small projects). Then you could easily use the default authorize anotation Heres a tutorial: OWIN AUTH

jollyjoyce1995
  • 322
  • 4
  • 15