gpt4 book ai didi

c# - 在 Identity 2.0 中成功实现自定义 UserManager

转载 作者:太空狗 更新时间:2023-10-29 20:30:33 24 4
gpt4 key购买 nike

我正在尝试在身份成员身份的自定义实现这个黑洞中导航。我现在的目标只是从我的 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。

同样,我现在的问题是在我的 Controller 中,我在尝试检索我的 UserManager 时得到 null

非常感谢任何帮助。

最佳答案

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

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

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

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

另外,终于找到了this great guide在我的谷歌搜索中排名靠后。它几乎回答了我所得到的一切。我需要实现一个具体类 UserManager<MyUser,int>

我希望这能对以后的人有所帮助。

关于c# - 在 Identity 2.0 中成功实现自定义 UserManager<IUser>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26366405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com