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

查看:124
本文介绍了找不到"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 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

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中的这段代码.

@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], 生存时间= Autofac.Core.Lifetime.RootScopeLifetime,共享=共享, 所有权= 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身份扩展名是此处,并且它已升级到更新版本已经.

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.

我无法在此处获得有关其使用方式的完整说明,但我可以肯定地将您指向我的开源项目

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定义的包版本完全相同的包版本

  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<User>作为基本AccountManager,并使用UserStore<User>作为管理器的基础存储.这里User类是Im使用的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
}

在这里,AccountManagerUserManager<T>的派生词,并且在其构造函数中包含IUserStore<User>.实际上,它为您提供了如何设计标识层的更多自由. :)

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. :)

希望这会有所帮助.

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

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