Asp.net mvc 如何将自定义数据添加到ASP.NET登录会话

Asp.net mvc 如何将自定义数据添加到ASP.NET登录会话,asp.net-mvc,authentication,Asp.net Mvc,Authentication,我需要添加一些自定义数据到“登录会话”。我不知道该怎么做。我应该在登录后创建一些特殊会话吗?我需要存储一些列表等。 我可以向身份声明添加额外数据,但只能添加字符串 我正在使用ASP.NETMVC标准身份验证 var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 您可以扩展标识用户: public class ApplicationUser

我需要添加一些自定义数据到“登录会话”。我不知道该怎么做。我应该在登录后创建一些特殊会话吗?我需要存储一些列表等。 我可以向身份声明添加额外数据,但只能添加字符串

我正在使用ASP.NETMVC标准身份验证

var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

您可以扩展
标识用户

public class ApplicationUser : IdentityUser
{
  public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
  {
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
     return userIdentity;
  }

  // insert the informations  that you want as new properties
     public string FullName { get; set; }
     public int EmployeeId { get; set; }
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
返回用户身份;
}
//插入所需的信息作为新属性
公共字符串全名{get;set;}
public int EmployeeId{get;set;}
}
或者,您也可以这样加入会话:

HttpContext context = System.Web.HttpContext.Current;
string result = context.Session["employeeInfo"]?.ToString();
if (string.IsNullOrEmpty(result))
{
  ApplicationUser user = context.GetOwinContext()
        .GetUserManager<ApplicationUserManager>()
        .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
   result = _employeeAppService.GetById(user.EmployeeId);
   context.Session.Add("employeeInfo", result);
 }
HttpContext=System.Web.HttpContext.Current;
字符串结果=context.Session[“employeeInfo”]?.ToString();
if(string.IsNullOrEmpty(result))
{
ApplicationUser user=context.GetOwinContext()
.GetUserManager()
.FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
结果=_employeeAppService.GetById(user.EmployeeId);
context.Session.Add(“employeeInfo”,结果);
}

按你的意愿对待结果。有什么疑问,尽管问吧。我希望它能有所帮助。

您可以扩展
标识用户

public class ApplicationUser : IdentityUser
{
  public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
  {
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
     return userIdentity;
  }

  // insert the informations  that you want as new properties
     public string FullName { get; set; }
     public int EmployeeId { get; set; }
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
返回用户身份;
}
//插入所需的信息作为新属性
公共字符串全名{get;set;}
public int EmployeeId{get;set;}
}
或者,您也可以这样加入会话:

HttpContext context = System.Web.HttpContext.Current;
string result = context.Session["employeeInfo"]?.ToString();
if (string.IsNullOrEmpty(result))
{
  ApplicationUser user = context.GetOwinContext()
        .GetUserManager<ApplicationUserManager>()
        .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
   result = _employeeAppService.GetById(user.EmployeeId);
   context.Session.Add("employeeInfo", result);
 }
HttpContext=System.Web.HttpContext.Current;
字符串结果=context.Session[“employeeInfo”]?.ToString();
if(string.IsNullOrEmpty(result))
{
ApplicationUser user=context.GetOwinContext()
.GetUserManager()
.FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
结果=_employeeAppService.GetById(user.EmployeeId);
context.Session.Add(“employeeInfo”,结果);
}

按你的意愿对待结果。有什么疑问,尽管问吧。我希望能有所帮助。

什么样的自定义数据?例如,我的另一个类的列表。或者只是整数、字符串等的列表,扩展IdentityUser有帮助吗?什么样的自定义数据?例如我的另一个类的实例列表。或者只是整数、字符串等的列表,扩展IdentityUser有帮助吗?关于ApplicationUser扩展。。如何在代码中访问这些属性(控制器、视图)?我可以使用User.Identity.,但我只能看到标准属性或我放入声明中的属性(在本例中只有字符串)。THX。如果你把这些新的属性,他们将成为代码>应用程序用户>代码>的一部分,如电子邮件、名称等。你将需要改变登记器来填充这些属性。因为我不完全是你的需要,我不能建议你,我认为更好的方法。但一个好的做法是扩展控制器类并在那里执行所有这些“管道”。您可以使用任何类型的属性,甚至是复杂的用户定义属性。检查此项:关于ApplicationUser扩展。。如何在代码中访问这些属性(控制器、视图)?我可以使用User.Identity.,但我只能看到标准属性或我放入声明中的属性(在本例中只有字符串)。THX。如果你把这些新的属性,他们将成为代码>应用程序用户>代码>的一部分,如电子邮件、名称等。你将需要改变登记器来填充这些属性。因为我不完全是你的需要,我不能建议你,我认为更好的方法。但一个好的做法是扩展控制器类并在那里执行所有这些“管道”。您可以使用任何类型的属性,甚至复杂的用户定义属性。请检查以下内容: