5

I am trying to authenticate without using identity. I have found a few articles describing how to do it in other versions however nothing for ASP.NET Core 2.

Below is what I have cobbled together. But when it gets to SignInAsync an exception is thrown InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
        {
            o.LoginPath = new PathString("/Account/Login/");
            o.AccessDeniedPath = new PathString("/Account/Forbidden/");

        });
        services.AddAuthentication();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

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

        app.UseAuthentication();
    }

    public async Task<IActionResult> Login()
    {

        var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, "joe nobody")
            };
        var identity = new ClaimsIdentity(claims, "MyCookieMiddlewareInstance");
        var principal = new ClaimsPrincipal(identity);

        //blows up on the following statement:
        //InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
        await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal); 

        return View();
    }

There is a Microsoft document for asp.net core v1.x (https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie) however the IApplicationBuilder.UseCookieAuthentication() is depreciated in v2 and haven't found any solution.

1
  • 1
    I'm not setup to try anything 2.0 related,but I'm very interested in your question since it will affect me. If you look at some of their unit tests, you may be able to get an idea of at least what objects you should call: github.com/aspnet/Identity/pull/1119/commits/… Jun 15, 2017 at 1:30

1 Answer 1

4

It looks like there are a few breaking changes with Auth 2.0 (https://github.com/aspnet/Announcements/issues/232)

The setup was correct however I needed to do two things:

  1. Use HttpContext.SignInAsync() (using Microsoft.AspNetCore.Authentication) instead of HttpContext.Authentication.SignInAsync()
  2. Use "AuthenticationTypes.Federation" as the authentication type (Note: other values don't seem to work and blank will result in the user name being set and IsAuthenticated to false) var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");

Below is the corrected code

In Startup.cs

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
        {
            o.LoginPath = new PathString("/Account/Login/");
            o.AccessDeniedPath = new PathString("/Account/Forbidden/");
        });
        services.AddAuthentication();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

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

In Controller

    using Microsoft.AspNetCore.Authentication;
    //...
    public async Task<IActionResult> Login()
    {
        var claims = new List<Claim>
        {
            new Claim(ClaimTypes.Name, "joe nobody")
        };
        var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");
        var principal = new ClaimsPrincipal(identity);
        await HttpContext.SignInAsync("MyCookieMiddlewareInstance", principal);

        return View();
    }
1
  • 5
    Rather than hard-coding AuthenticationTypes.Federation you should probably reference TokenValidationParameters.DefaultAuthenticationType, which is hard-coded to that value in Microsoft.IdentityModel.Tokens... which, for some reason, is hidden away in the AzureAD extensions project. Clear as mud...
    – McGuireV10
    Jan 8, 2018 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.