【问题标题】:Successfully implementing custom UserManager<IUser> in Identity 2.0在 Identity 2.0 中成功实现自定义 UserManager<IUser>
【发布时间】:2014-10-15 03:35:33
【问题描述】:

我正在尝试浏览作为身份成员身份自定义实现的黑洞。我现在的目标只是从我的ApiController 获取此行以正确检索我的 UserManager:

public IHttpActionResult Index()
{
    var manager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager<MyUser,int>>();
    //manager is null
}

这是我的设置。在我的Startup 的配置中,我设置了 WebApi 并添加了我的 OwinContext:

app.CreatePerOwinContext<UserManager<MyUser,int>>(Create);

我的创建方法:

public static UserManager<User,int> Create(IdentityFactoryOptions<UserManager<MyUser,int>> options, IOwinContext context)
{
   return new UserManager<MyUser,int>(new UserStore(new DbContext()));
}

剩下的是我能做的最基本的实现。

我的用户:

public class MyUser : IUser<int>
{
    public int Id { get; set; }
    public string UserName { get; set; }
}

我的用户商店:

 public class MyUserStore : IUserStore<MyUser,int>
 {
    private IdentityDbContext context;
    public MyUserStore(IdentityDbContext c)
    {
       context = c;
    }

    //Create,Delete, etc implementations
 }

MyDbContext:

 public class MyDbContext : IdentityDbContext
 {
     public MyDbContext() : base("Conn")
     {

     }
 }

这一切都是为了了解 Identity 的工作原理,我非常确信没有人真正知道这一点。我希望最终能够完全自定义我的用户和角色,避免使用 Microsoft 的 IdentityUser。

同样,我现在的问题是,在我的控制器中,我在尝试检索我的 UserManager 时收到 null

非常感谢任何和所有帮助。

【问题讨论】:

  • 关于“学习身份如何工作的相当大的声明,我非常确信没有人真正知道这一点”。源代码知道。源代码可通过反编译器获得。
  • 当我找不到很多例子时,这更多是表达我的沮丧

标签: c# asp.net-web-api asp.net-identity owin asp.net-identity-2


【解决方案1】:

我有几个错误,但主要是我的 Startup 类看起来像:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseWebApi(new WebApiConfig());

        app.CreatePerOwinContext(MyDbContext.Create);
        app.CreatePerOwinContext<MyUserManager>(MyUserManager.Create);
    }
}

当然,OWIN 管道会在触发 WebAPI 调用后插入我的依赖项。切换这些是我的主要问题。

此外,我终于在 Google 搜索中找到了this great guide。它几乎回答了我遇到的所有问题。我需要实现一个具体的类,它是 UserManager&lt;MyUser,int&gt;

我希望这能够帮助以后的人。

【讨论】:

    【解决方案2】:

    您使用的是 MyIdentity.User 还是 MyIdentity.MyUser?在您的示例中,您有两个不同的对象传递到 TUser 参数中。

    我测试了您的实现,在检索 UserManager 的注册实例时没有遇到问题。

    要完全自定义您的用户和角色,您最终需要编写 UserManager 的新实现,以使您的身份验证、声明、角色、方法调用等工作。

    如果您想了解 Identity 的工作原理,我建议您使用 UserManager 和 IdentityUser 的默认实现。先让它发挥作用。

    Here is a good tutorial

    最终,创建您自己的用户来实现 IdentityUser。所有 UserManager 功能都将起作用,您将可以访问您的新属性。附加属性将自动构建到 IdentityDbContext 生成的 AspnetUser 表中。

    public MyIdentityUser : IdentityUser
    {
       public int Id {get; set;}
       public string CustomUserId {get; set;}
       public string UserId { get; set; }
    }
    

    或者您可以使用对其他对象的外键引用开始构建您的自定义 MyIdentityUser。

    public MyIdentityUser : IdentityUser
    {
       public virtual UserProperties Properties { get; set; }
    }
    

    这有帮助吗?

    【讨论】:

    • 谢谢斯科特。我已经改变了我的课程,当我编辑问题时,我在保持一致性方面做得很差。我的主要问题是我自己在答案中发布的,但您的问题也很有帮助。您知道使用 IdentityUser 与 IUser 的好处吗?
    • IUser 由 IdentityUser 实现,IdentityUser 是 Id 和 UserName 属性的协定。
    • (续)实现 IUser 需要 UserManager 接受的 TUser 泛型对象。因此,您的 MyUser 对象被接受为通用对象类型,但 UserManager 具有更多方法,例如“ChangePasswordAsync(TUser user, string password)”,这些方法依赖于作为通用对象一部分的这些属性。 IdentityUser 包含这些属性。使用 IdentityUser 作为 MyUser 对象的基类可以节省您必须自己声明它们或覆盖 UserManager 方法以使它们与您的自定义属性一起使用的时间。
    猜你喜欢
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多