2017-08-19 22 views
7

Sitemde Çerez Kimlik Doğrulaması eklemek için here yönergelerini izlemeyi deniyorum.InvalidOperationException: Hiçbir IAuthenticationSignInHandler, şema için oturum açma işlemek üzere yapılandırıldı: MyCookieAuthenticationScheme

Şimdiye kadar aşağıdaki eklemiş:

Invoke the UseAuthentication method in the Configure method of the Startup.cs file:

app.UseAuthentication(); 

Invoke the AddAuthentication and AddCookie methods in the ConfigureServices method of the Startup.cs file:

services.AddAuthentication("MyCookieAuthenticationScheme") 
    .AddCookie(options => { 
    options.AccessDeniedPath = "/Account/Forbidden/"; 
    options.LoginPath = "/Account/Unauthorized/"; 
}); 

benim giriş kodu Sonra

await HttpContext.SignInAsync("MyCookieAuthenticationScheme", principal); 

principle bir ClaimsPrincipal olduğunu var.

siteme giriş ve ben hata alıyorum yukarıdaki hattını arayın: kaçırdığım ne

InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: MyCookieAuthenticationScheme

?

cevap

20

Varsayılan şemanın "MyCookieAuthenticationScheme" (bu, AddAuthentication numaralı ilk argüman) olmasını istediniz, ancak bu ada sahip bir kimlik doğrulama işleyicisi eklemediniz. AddCookies'u aradığınızda, işleyiciyi "Çerezler" şemasına eklediniz (bu varsayılan değerdir).

services.AddAuthentication("MyCookieAuthenticationScheme") 
    .AddCookie("MyCookieAuthenticationScheme", options => 
    { 
     options.AccessDeniedPath = "/Account/Forbidden/"; 
     options.LoginPath = "/Account/Unauthorized/"; 
    }); 

bu yazı daha iyi ilkeller anlamak için bakınız::

Sen için kodunuzu değiştirmeniz gerekir

https://digitalmccullough.com/posts/aspnetcore-auth-system-demystified.html

+0

Teşekkür ederiz! Günün çoğunu anlamaya çalışıyorum. Orada çok sayıda örnek var ama kimlik doğrulama işleyicisini kaçırıyorlar. – Tom

0

deneyin Bu bana yardımcı çağrı services.AddAuthentication( yerini değiştirmek için. builder.Populate(services) önce services.AddAuthentication yerleştirerek Projemde

public IServiceProvider ConfigureServices(IServiceCollection services) 
{ 
    ... 
    var builder = new ContainerBuilder(); 
    builder.RegisterModule(new HowResolveDependencies()); 

    services.AddTransient<IExceptionHandler, ExceptionToResponseWriter>(); 

    builder.Populate(services); 

    services.AddAuthentication(AuthConsts.MainAuthScheme) 
       .AddCookie(
    ... 
} 

, sorunu çözdü.