'IdentityContext'找不到(您是否缺少 using 指令或程序集引用) [英] 'IdentityContext' could not be found (are you missing a using directive or an assembly reference)

查看:85
本文介绍了'IdentityContext'找不到(您是否缺少 using 指令或程序集引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am migrate the project https://github.com/attilah/AngularJSAuthentication to using mongodb driver verson 2 , and I am in the final step here:

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 描述 Project File Line Suppression State错误 CS0246 找不到类型或命名空间名称IdentityContext"(您是否缺少 using 指令或程序集引用?)AngularJSAuthentication.API D:\Projects\AngularJSAuthentication-master\AngularJSAuthentication.API\ApplicationIdentityContext.cs

这是我当前的源代码

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

更新

@Swagata Prateek,感谢您的代码,但是当我运行它时,它会停止工作,将我定向到 startup.cs 中的这段代码

@Swagata Prateek, thank for your code,however when I run the It stop working in direct me to this block of code in 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>()
            };

错误

Autofac.Core.DependencyResolutionException"类型的异常发生在 Autofac.dll 中,但未在用户代码中处理

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

附加信息:在激活一个具体注册.有关详细信息,请参阅内部异常.注册:Activator = SimpleAuthorizationServerProvider(ReflectionActivator), 服务 =[Microsoft.Owin.Security.OAuth.IOAuthAuthorizationServerProvider],Lifetime = Autofac.Core.Lifetime.RootScopeLifetime,Sharing = Shared,所有权 = OwnedByLifetimeScope

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 实现中使用了相同的示例,而您缺少该类的原因是因为示例很漂亮旧的和它所依赖的 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. :)

希望这会有所帮助.

这篇关于&amp;#39;IdentityContext&amp;#39;找不到(您是否缺少 using 指令或程序集引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆