且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

'IdentityContext'找不到(您是否缺少 using 指令或程序集引用)

更新时间:2023-02-17 20:26:06

我个人在 Mongodb 上的 Asp.net Identity 实现中使用了相同的示例,而您缺少该类的原因是因为示例很漂亮旧的和它所依赖的 mongodb 身份扩展是 here 并且它已经升级到更新版本已经.

I personally used the same example for my Asp.net Identity implementation over Mongodb and the reason you're missing that class is because of the fact the sample is pretty old and the Identity extension for mongodb it depends on is here and it has progressed to updated versions already.

我无法在此处完整描述我如何使用它,但我肯定可以向您指出我的开源项目 这里 我从你提到的例子中学到的.我希望它能解决您使用 Mongodb 实现 Asp.net Identity 的问题.

I can't go with the full description here on how I used it but I surely can point you to my open source project here where I've learned form the example that you mentioned. I hope it would solve your problem on implementing Asp.net Identity with Mongodb.

  1. 如果您想拥有与 Attila Hajdrik 在 git repo 中编写的完全相同的解决方案,请确保您拥有与 AspNet.Identity.MongoDB 定义的完全相同的软件包版本 这里.因为库本身现在已自行升级,我认为您要么更新了所有 nuget 包,要么根据需要重新创建了在 github 存储库中编写的整个解决方案.在这两种情况下,您最终可能会得到不想使用的 AspNet.Identity.MongoDB 版本.这应该是满足您需求的最短和最简单的解决方案.

  1. If you want to have the exactly same solution that Attila Hajdrik wrote in the git repo please make sure you have exactly the same package version the AspNet.Identity.MongoDB defined here. Because the library itself is now upgraded itself and I presume you either updated all the nuget packages or recreated the whole solution written in the github repo according to your need. In both cases, you might end up with a version of AspNet.Identity.MongoDB you don't want to use. This one should be the shortest and easiest solution for your need.

现在在我的 github 存储库中提到我的解决方案.我使用了我自己的 IAccountContext,我使用了 UserManager 作为我的基本 AccountManager 和 UserStore 作为我的管理器基础存储.这里User 类是我使用的Identity 类,它派生自IdentityUser.

Now on my solution mentioned above from my github repo. I used my own IAccountContext and I used UserManager<User> as my base AccountManager and UserStore<User> as my underlying store for the manager. Here User class is the Identity class that Im using which is derived from IdentityUser.

从技术上讲,您可以轻松构建自己的上下文,而不会真的必须完全依赖给定的例子.

Technically you can build your own context out easily and you don't really have to rely on the given example to the fullest.

我的示例 AccountContext 将是:

My sample AccountContext would be :

public class AccountContext : IAccountContext
{        
    private readonly IDbContext dbContext;
    private readonly AccountManager accountManager;

    public AccountContext(
        IDbContext dbContext,         
        AccountManager accoutnManager)
    {
        this.dbContext = dbContext;
        this.accountManager = accoutnManager;        
    }
// Your own stuff here
}

这里,AccountManager 是一个 UserManager 派生类,它在它的构造函数中使用了一个 IUserStore.它实际上为您提供了更多的***来设计您的身份层.:)

Here, AccountManager is a UserManager<T> derivative and it takes a IUserStore<User> in it's constructor. It practically gives you more freedom on how you want to design your Identity layer. :)

希望这会有所帮助.