1

Currently I have a problem that I need to solve to avoid duplicate code in my software. I would like that by the time the login was performed, the system would store some information so that I did not have to go all the time in the query database. I would need to store, for example, information from two classes:

USER

public class User
{
    public Guid Id { get; set; }

    public String Name { get; set; }    

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
}

SCHOOL

public class School
{
    public Guid Id { get; set; }

    public String Name { get; set; }    

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    public int Code { get; set; }
}

I constantly use information from these two in my software, and currently every time I load them from the database. So I would like to know if there is a way for me to store the information at the time I log in so that I do not have to query the database. My login code:

if (user != null)
{
    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, 
      FormsAuthentication.Encrypt(new FormsAuthenticationTicket(login.Email.ToLower(), false, 60))));
    //Store school and user information here.
    return RedirectToAction("Dashboard", "Home", new { area = "" });
}

Is there any way to store this information and retrieve them the same as the User.Identity.Name, for example?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Leomar de Souza
  • 677
  • 10
  • 23
  • 1
    If you have a single server you can store these in the asp.net cache. You would still need a secure way to identify the user and read the correct profile. If you have multiple servers you can look into a distributed cache like redis. The same caveats apply. – No Refunds No Returns Dec 08 '18 at 23:54
  • Why did you tag two different versions of ASP.NET MVC in your question? – mason Dec 09 '18 at 00:31
  • @mason because I need a response regardless of the version of ASP.NET, it is not something specific to the version. It's wrong? – Leomar de Souza Dec 09 '18 at 00:33
  • @NoRefundsNoReturns It seems to me a good idea. I will research more about this, as it may solve my problem :) – Leomar de Souza Dec 09 '18 at 00:35
  • You should only tag specific versions in your question if your problem is directly related to one of those versions. I doubt you're even using either of those versions, you're most likely using ASP.NET MVC 5, which you can confirm by looking at the version numbers of the assembly references. – mason Dec 09 '18 at 00:35

3 Answers3

1

there are many different solution for your requirement:

Caching

You can use in memory cache (like asp.net cache) if your app is running on one server. If more than one then you can use a distributed cache something like Redis, or Memcached. You can also get some free online server for test purposes as well. If you choose caching you need a good strategy to invalidate the cache as well because cache without a good invalidation strategy can be very destructive for your application.

No SQL databases

Another solution is No SQL databases which are much faster than relational databases.

Web storage

If your data is not sensitive, you can store a small amount of data in web storage of the browser for each user with some considerations.

Meysam
  • 555
  • 1
  • 3
  • 16
0

Use session, after login set

Session["user"] = user;
Session["school"] = school; 

and then use it wherever you want How to use sessions in an ASP.NET MVC 4 application?

Ivan Valadares
  • 869
  • 1
  • 8
  • 18
0

You could use the Session Object

https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.session?view=aspnet-mvc-5.2

https://code.msdn.microsoft.com/How-to-create-and-access-447ada98

This object allow you to use the information of a User in multiple controllers. It also allows you to add to it an expired type. By default it's 30min.

HNL
  • 103
  • 13