【发布时间】: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