在https://developers.facebook.com/上创建一个Facebook应用程序,并进行必要的设置。
安装NuGet软件包Microsoft.AspNetCore.Authentication.Facebook。
将以下代码添加到Startup.cs文件中的ConfigureServices方法中。此代码将Facebook身份验证添加到应用程序中。
services.AddAuthentication().AddFacebook(facebookOptions => { facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"]; facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"]; });
app.UseAuthentication();
"Authentication": { "Facebook": { "AppId": "your-app-id", "AppSecret": "your-app-secret" } }
[AllowAnonymous] public IActionResult ExternalLoginCallback(string returnUrl = null, string remoteError = null) { if (remoteError != null) { ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}"); return View("Login"); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return RedirectToAction(nameof(Login)); } var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true); if (result.Succeeded) { return RedirectToLocal(returnUrl); } if (result.RequiresTwoFactor) { return RedirectToAction(nameof(LoginWith2fa), new { returnUrl, rememberMe }); } if (result.IsLockedOut) { return RedirectToAction(nameof(Lockout)); } else { ViewData["ReturnUrl