Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MVC 5 Identity按ID获取IdentityUser_C#_Asp.net Mvc_Asp.net Mvc 5_Asp.net Identity 2 - Fatal编程技术网

C# 使用MVC 5 Identity按ID获取IdentityUser

C# 使用MVC 5 Identity按ID获取IdentityUser,c#,asp.net-mvc,asp.net-mvc-5,asp.net-identity-2,C#,Asp.net Mvc,Asp.net Mvc 5,Asp.net Identity 2,创建另一个模型时,我将活动用户的Id存储为作者,该字符串类似于:00b9c6aa-010c-4cbd-ac16-c72d05e7906a。在另一个视图中,我想为每个推荐显示与该Id关联的名称和其他用户信息。该视图将包含来自许多不同用户的信息,而不仅仅是活跃记录的用户。我不知道如何通过Id获取用户的信息 如何通过Id获取IdentityUser 包含用户的所有表都是默认值加上一些附加列。在名为AspNetUsers的表中,我得到了Id、FirstName、Email等。Id是键值,我想知道如何(在

创建另一个模型时,我将活动用户的
Id
存储为
作者
,该字符串类似于:
00b9c6aa-010c-4cbd-ac16-c72d05e7906a
。在另一个视图中,我想为每个
推荐
显示与该
Id
关联的名称和其他用户信息。该视图将包含来自许多不同用户的信息,而不仅仅是活跃记录的用户。我不知道如何通过
Id
获取用户的信息

如何通过
Id
获取
IdentityUser

包含用户的所有表都是默认值加上一些附加列。在名为
AspNetUsers
的表中,我得到了
Id
FirstName
Email
等。Id是键值,我想知道如何(在
推荐
的控制器中)检索给定
Id
的信息 //配置此应用程序中使用的应用程序用户管理器。UserManager在ASP.NET标识中定义,并由应用程序使用

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);
    }
}
您可以像下面这样获得已登录的用户

var user = await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync());

我假设您已经扩展了MVC项目模板生成的默认上下文?您是否创建了新表?如果是,请添加到问题中。我已添加了问题。是的,但您创建了一个
证明表,其中包含创建该表的用户的
Id
。这是作为
ApplicationUser
表的外键完成的吗?不是。我不熟悉ASP.Net和SQL。如果我将其更改为外键,是否有一种简单的方法来获取每个用户的信息?是的-您将能够查询
推荐表
,并在单个数据库操作中检索相关的用户详细信息。如何查询您的
推荐表?是否通过实体框架?这仅获取登录用户。我相信海报希望获得与其他表中记录相关的用户详细信息。var user=await UserManager.FindByIdAsync(“00b9c6aa-010c-4cbd-ac16-c72d05e7906a”);当var user=await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync())时,可使用任何用户Id;仅用于已登录的用户。
var user = await UserManager.FindByIdAsync("00b9c6aa-010c-4cbd-ac16-c72d05e7906a");
var user = await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync());