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

时间秒杀一切 提交于 2019-12-08 12:22:43

问题


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)

        {
        }
    }
}

Then I get this error

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

Here is my current source code

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

UPDATED

@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>()
            };

Error

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


回答1:


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.

Update

  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.

  2. 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.

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
}

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

Hope this helps.




回答2:


A lot of this package versions are old and can be an issue be compatibility. If nothing is preventing you from updating, please do update the packages to latest versions.

When you do update, just FYI that IdentityContext does not exists anymore in the AspNet.Identity.MongoDb package/library. See source code in Github: https://github.com/g0t4/aspnet-identity-mongo



来源:https://stackoverflow.com/questions/38914303/identitycontext-could-not-be-found-are-you-missing-a-using-directive-or-an-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!