简体   繁体   English

在asp.net mvc 4应用程序中使用authentification中的会话变量

[英]Using a session variable in authentification within asp.net mvc 4 application

i have an asp.net mvc4 application, in which i have this class: 我有一个asp.net mvc4应用程序,我有这个类:

public class Internaute {
  public int Id { get; set; }
  public string Name { get; set; }
  public string Login { get; set; }
  public string Password { get; set; }
}

then, when a user connect, i get its informations by storing it in a session variable like this: 然后,当用户连接时,我通过将其存储在如下的会话变量中来获取其信息:

Session["user"] = myInternaute;

And i used these informations ,for example, like this: 我使用这些信息,例如,像这样:

@{
  Internaute myInternaute = (Internaute)Session["user"];
  string login = myInternaute.Login;
  string pwd = myInternaute.Password;
}

I test the autorization of the user to acces by 我测试用户的自动化以进行访问

Internaute myInternaute = (Internaute)Session["user"];
   if(myInternaute == null) return RedirectToAction("Index");

So i have these questions: 所以我有这些问题:

  1. Is it a good way to proceed by a session variable? 这是一个通过会话变量进行的好方法吗?
  2. Is there another idea to do this, because the session were lost. 是否还有其他想法,因为会话丢失了。
  3. Does this idea have some advantages? 这个想法有一些优势吗?

Thanks, 谢谢,

Is it a good way to proceed by a session variable? 这是一个通过会话变量进行的好方法吗?

Yes your code looks good except you should check for null whenever you get the value from Session to make sure its not null. 是的,您的代码看起来很好,除非您从Session获取值时应检查null以确保它不为null。 Also, do you really need Password stored in session? 另外,你真的需要在会话中存储密码吗? Its not a good idea to store it as string in Session. 将它作为字符串存储在Session中不是一个好主意。

Is there another idea to do this, because the session were lost. 是否还有其他想法,因为会话丢失了。

If I understand your question correctly, yes your session data will be lost on Session timeout. 如果我正确理解您的问题,那么您的会话数据将在会话超时时丢失。 If you want you can increase the session timeout in the web.config file. 如果需要,可以在web.config文件中增加会话超时。

Does this idea have some advantages? 这个想法有一些优势吗?

You will have some basic data about the user readily available in Session instead of querying the database but you should make sure that the Internaute class remains lightweight. 您将获得有关在Session中随时可用的用户的一些基本数据,而不是查询数据库,但您应该确保Internaute类保持轻量级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM