How are Authentication type names registered in as

2019-06-15 13:21发布

问题:

So I am updating an Open Source asp.net Identity provider for MongoDB to work with Asp.Net Identity 3.0 (aka vnext). So far I have been able to register the provider and create users but when using the SignInManager if a correct UserName/Pass is provided I get the error

InvalidOperationException: The following authentication types were not accepted: Microsoft.AspNet.Identity.Application

I have tracked down the error to here https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNet.PipelineCore/DefaultHttpResponse.cs

but I can't seem to see where the SignInContext.Accepted name is getting added to the SignInContext.

I am using the Alpha-2 versions of all of the vnext libraries that are being used in VS14 CTP2

Below is my Startup.cs

public void Configure(IBuilder app)
    {
        try {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
            var configuration = new Configuration();
            configuration.AddJsonFile("config.json");
            configuration.AddEnvironmentVariables();

         //   app.UseLogRequests("try");
            app.UseErrorPage(ErrorPageOptions.ShowAll);
            app.UseServices(services =>
            {

                services.AddIdentity<MyUser>()
                .AddMongoDB<MyUser>(configuration.Get("Data:MongoIdentity:ConnectionString"), configuration.Get("Data:MongoIdentity:DBName"))
                .AddHttpSignIn<MyUser>();


                // Add MVC services to the services container
                services.AddMvc();
            });

            // Add static files to the request pipeline
            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Index" });

                routes.MapRoute(
                    name: "api",
                    template: "api/{controller}/{action}",
                    defaults: new { action = "Index" });
            });

            // Add cookie-based authentication to the request pipeline
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
            });
        }
        catch (Exception ex)
        {
            Console.Write(ex.ToString());
            throw;
        }
    }

回答1:

Turns out I had setup MVC before the CookieAuthentication so the AuthenticationType was not registered in MVC when it tried to authenticate the user. As MVC depends upon the authentication I just had to bump it up and register it before MVC.