使用 MVC 5 Identity 按 ID 获取 IdentityUser
Get IdentityUser by ID using MVC 5 Identity
创建另一个模型时 Testimonial
我将活动用户的 Id
存储为 Author
,它是一个类似于 00b9c6aa-010c-4cbd-ac16-c72d05e7906a
的字符串。在另一个视图中,我想为每个 Testimonial
显示与该 Id
关联的名称和其他用户信息。该视图将包含来自许多不同用户的信息,而不仅仅是主动登录的用户。我不知道如何通过 Id
.
获取用户的信息
如何通过 Id
获得 IdentityUser
?
包含用户的所有 tables 都是默认值加上一些额外的列。在名为 AspNetUsers
的 table 中,我有 Id
、FirstName
、Email
等。Id
是键值,我想知道如何(在 Testimonial
的控制器内)检索给定 Id
.
的信息
IdentityConfig.cs
// 配置此应用程序中使用的应用程序用户管理器。 UserManager 在 ASP.NET Identity 中定义并由应用程序使用。
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) :
base(userManager, authenticationManager) { }
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
}
}
在您的控制器中定义以下属性
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
private ApplicationSignInManager _signInManager;
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set { _signInManager = value; }
}
您可以在controller中使用如下方法
var user = await UserManager.FindByIdAsync("00b9c6aa-010c-4cbd-ac16-c72d05e7906a");
可以像下面这样获取登录用户
var user = await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync());
创建另一个模型时 Testimonial
我将活动用户的 Id
存储为 Author
,它是一个类似于 00b9c6aa-010c-4cbd-ac16-c72d05e7906a
的字符串。在另一个视图中,我想为每个 Testimonial
显示与该 Id
关联的名称和其他用户信息。该视图将包含来自许多不同用户的信息,而不仅仅是主动登录的用户。我不知道如何通过 Id
.
如何通过 Id
获得 IdentityUser
?
包含用户的所有 tables 都是默认值加上一些额外的列。在名为 AspNetUsers
的 table 中,我有 Id
、FirstName
、Email
等。Id
是键值,我想知道如何(在 Testimonial
的控制器内)检索给定 Id
.
IdentityConfig.cs // 配置此应用程序中使用的应用程序用户管理器。 UserManager 在 ASP.NET Identity 中定义并由应用程序使用。
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) :
base(userManager, authenticationManager) { }
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
}
}
在您的控制器中定义以下属性
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
private ApplicationSignInManager _signInManager;
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set { _signInManager = value; }
}
您可以在controller中使用如下方法
var user = await UserManager.FindByIdAsync("00b9c6aa-010c-4cbd-ac16-c72d05e7906a");
可以像下面这样获取登录用户
var user = await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync());