2017-06-14 12 views
6

मैं पहचान का उपयोग किये बिना प्रमाणित करने की कोशिश कर रहा हूं। मुझे कुछ संस्करण मिलते हैं जो अन्य संस्करणों में इसे कैसे करें, हालांकि एएसपी.नेट कोर 2 के लिए कुछ भी नहीं।पहचान के बिना कुकी मिडलवेयर एएसपी.नेट कोर v2

नीचे मैंने जो कुछ किया है, वह नीचे है। लेकिन जब यह एक अपवाद SignInAsync जाता 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(); 
    } 

वहाँ asp.net कोर v1.x (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie) के लिए एक Microsoft दस्तावेज है फेंक दिया जाता है लेकिन IApplicationBuilder.UseCookieAuthentication() वी 2 में मूल्यह्रास हुआ है और नहीं मिला है कोई भी समाधान।

+1

मैं कोशिश से संबंधित कुछ भी 2.0 सेटअप नहीं हूँ, लेकिन मैं 'में मैं आपके प्रश्न में बहुत रूचि रखता हूं क्योंकि इससे मुझे प्रभावित होगा। यदि आप उनके कुछ यूनिट परीक्षणों को देखते हैं, तो आप कम से कम किस वस्तु को कॉल करना चाहते हैं, इसका विचार प्राप्त करने में सक्षम हो सकते हैं: https://github.com/aspnet/Identity/pull/1119/commits/2bdfdb3220b3aca7513455652617af5685d5293c –

उत्तर

5

ऐसा लगता है कि प्रमाणीकरण 2,0 (https://github.com/aspnet/Announcements/issues/232)

के साथ कुछ तोड़ने परिवर्तन सेटअप सही था लेकिन मैं जरूरत दो काम करने देखते हैं जैसे: HttpContext.Authentication.SignInAsync()

  1. उपयोग HttpContext.SignInAsync() (using Microsoft.AspNetCore.Authentication) के बजाय
  2. प्रमाणीकरण प्रकार के रूप में "AuthenticationTypes.Federation" का उपयोग करें (नोट: अन्य मान काम नहीं करते हैं और खाली होने पर उपयोगकर्ता नाम सेट किया जाएगा और गलत पर प्रमाणीकृत किया जाएगा) var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");

नीचे को सही कोड है

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?}"); 
     }); 
    } 

नियंत्रक

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

हार्ड की बजाय कोडिंग 'प्रमाणीकरण टाइप्स। फेडरेशन' आपको शायद 'टोकन वैलिडेशन पैरामीटर' डिफॉल्ट प्रमाणीकरण टाइप 'का संदर्भ देना चाहिए, जो उस मान को हार्ड-कोड किया गया है [' माइक्रोसॉफ्ट.इडेन्टिटीमोडेल। टोकन्स'] (https://github.com/AzureAD/azure-activedirectory- पहचान-मॉडल-एक्सटेंशन-टू-डॉटनेट/ब्लॉब/मास्टर/src/माइक्रोसॉफ्ट.इडेन्टिटीमोडेल। टोकन/टोकन वैलिडेशन पैरामीटर सीएस) ... जो, किसी कारण से, AzureAD एक्सटेंशन प्रोजेक्ट में छिपा हुआ है। कीचड़ की तरह साफ़... – McGuireV10

संबंधित मुद्दे