_signInManager.GetExternalLoginInfoAsync() 始終返回帶有開放 ID 的 null 到 azure 廣告
為什麼signinmanager getexternallogininfoasync 方法總是返回null?
我將 VS2015 與帶有單個使用者帳戶的 MVC 的預設 asp.net 核心(非框架)項目一起使用**(這是一項要求)**。使用第三方登錄的目的是允許使用者自行註冊。基於角色的授權將由 asp.net 身份使用通過 Azure AD 註冊提供的身份來處理。
如果以下對登錄管理器的解釋不正確,請糾正我。此方法應提供有關外部登錄的詳細資訊,並返回一個包含使用者提供的聲明的聲明主體對象,該聲明由身份提供者提供。
我已使用以下指南在我的 Startup.cs 中設置 OpenIdConnectAuthentication(下面的類部分)
當我啟動外部登錄提供程序時,它會將我定向到組織登錄頁面並成功。
但是,應由 signinmanager 方法填充的變數資訊為空
如果我在回調類中放置一個斷點,則填充使用者並且 IsAuthenticated 變數為真。
我可以驅動允許使用者自己在應用程序中註冊的功能,但是,這是我第一次嘗試實現第三方登錄,我想了解我在這一點上做錯了什麼。
啟動.cs
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); // Add Authentication services. services.AddAuthentication(sharedOptions => { sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; }); //services.AddDistributedMemoryCache(); //services.AddSession(); services.AddMvc(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseApplicationInsightsRequestTelemetry(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseApplicationInsightsExceptionTelemetry(); app.UseStaticFiles(); app.UseIdentity(); // Configure the OWIN pipeline to use cookie auth. app.UseCookieAuthentication( new CookieAuthenticationOptions()); //Add external authentication middleware below.To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715 app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme, CallbackPath = "/signin-oidc", ClientId = Configuration["AzureAD:ClientId"], Authority = String.Format(Configuration["AzureAd:AadInstance"], Configuration["AzureAd:Tenant"]), ResponseType = OpenIdConnectResponseType.IdToken, PostLogoutRedirectUri = Configuration["AzureAd:PostLogoutRedirectUri"], Events = new OpenIdConnectEvents { //OnRemoteFailure = OnAuthenticationFailed, } }); //app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }外部登錄
public IActionResult ExternalLogin(string provider, string returnUrl = null) { // Request a redirect to the external login provider. var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); return Challenge(properties, provider); }
過去,此方法產生空值時已報告了多個問題。開箱即用的任何受支持的身份驗證方法都不會發生這種情況。至少在將 OAuth 與 azure AD 一起使用並遵循文章中提供的方法時,這是一個問題。但是,有一種解決方法仍然允許使用預設項目類型。只需將生成 ExternalLoginInfo 變數 (info) 的方法替換為使用 User 原則構造的您自己的 ExternalLoginInfo 類。
ExternalLoginInfo info = new ExternalLoginInfo(User, "Microsoft", User.Claims.Where(x=>x.Type== "http://schemas.microsoft.com/identity/claims/objectidentifier").FirstOrDefault().Value.ToString(), "Microsoft" );ASP.NET MVC 5(VS2013 最終版):使用 OWIN 登錄 Facebook 失敗(loginInfo 為空)
