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

'IdentityContext' could not be found (are you missing a using directive or an assembly reference)

我正在将项目 https://github.com/attilah/AngularJSAuthentication 迁移到使用 mongodb 驱动程序版本 2,我现在处于最后一步:

namespace AngularJSAuthentication.API
{
    using AspNet.Identity.MongoDB;
    using Entities;
    using MongoDB.Driver;

    public class ApplicationIdentityContext : IdentityContext
    {
        public ApplicationIdentityContext(IMongoContext mongoContext)
            : this(mongoContext.Users, mongoContext.Roles)
        {
        }

        public ApplicationIdentityContext(IMongoCollection<User> users, IMongoCollection<Role> roles)
            : base(users, roles)

        {
        }
    }
}

然后我得到这个错误

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'IdentityContext' could not be found (are you missing a using directive or an assembly reference?) AngularJSAuthentication.API D:\Projects\AngularJSAuthentication-master\AngularJSAuthentication.API\ApplicationIdentityContext.cs

这是我当前的源代码

https://github.com/skanel/Angular2-WebApi2-Mongodb2-Authentication

已更新

@Swagata Prateek,感谢您的代码,但是当我 运行 它停止工作时,它会指示我进入 startup.cs

中的这段代码
private void ConfigureOAuth(IAppBuilder app, IContainer container)
        {
            var OAuthServerOptions = new OAuthAuthorizationServerOptions
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
                Provider = container.Resolve<IOAuthAuthorizationServerProvider>(),
                RefreshTokenProvider = container.Resolve<IAuthenticationTokenProvider>()
            };

错误

An exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll but was not handled in user code

Additional information: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = SimpleAuthorizationServerProvider (ReflectionActivator), Services = [Microsoft.Owin.Security.OAuth.IOAuthAuthorizationServerProvider], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope

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

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

更新

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

  2. 现在从我的 github 回购开始我上面提到的解决方案。我使用了自己的 IAccountContext,并使用 UserManager<User> 作为我的基本 AccountManager,并使用 UserStore<User> 作为我的经理的基础商店。这里 User class 是我使用的身份 class 派生自 IdentityUser.

从技术上讲,您可以轻松地构建自己的上下文,但您没有 真的必须充分依赖给定的例子。

我的示例 AccountContext 将是:

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<T> 导数,它的构造函数中有一个 IUserStore<User>。它实际上给了你更多的自由来设计你的身份层。 :)

希望这对您有所帮助。

这个包的很多版本都是旧的,可​​能是兼容性问题。如果没有什么阻止您更新,请将软件包更新到最新版本。

当您进行更新时,仅供参考,IdentityContext 不再存在于 AspNet.Identity.MongoDb package/library 中。请参阅 Github 中的源代码:https://github.com/g0t4/aspnet-identity-mongo