我偶然发现了一个问题,即Account/AccessDenied/
在向当前登录用户添加社交媒体身份验证时,应用程序会重定向用户.它似乎在用户第一次登录时工作,然后通过尝试添加另一个身份验证方法,它将用户返回到 Account/AccessDenied?ReturnUrl=%2Fmanage%2Flinklogincallback
.
我的猜测是[Authorize]属性出了问题,但只是第二次尝试添加外部认证方法.
ManageController
[Authorize] public class ManageController : Controller { // // POST: /Manage/LinkLogin [HttpPost] [ValidateAntiForgeryToken] public IActionResult LinkLogin(string provider) { // Request a redirect to the external login provider to link a login for the current user var redirectUrl = Url.Action("LinkLoginCallback", "Manage"); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User)); return Challenge(properties, provider); } // // GET: /Manage/LinkLoginCallback [HttpGet] public async TaskLinkLoginCallback() { var user = await GetCurrentUserAsync(); if (user == null) { return View("Error"); } var info = await _signInManager.GetExternalLoginInfoAsync(await _userManager.GetUserIdAsync(user)); if (info == null) { return RedirectToAction(nameof(ManageLogins), new { Message = ManageMessageId.Error }); } var result = await _userManager.AddLoginAsync(user, info); var message = result.Succeeded ? ManageMessageId.AddLoginSuccess : ManageMessageId.Error; return RedirectToAction(nameof(ManageLogins), new { Message = message }); } }
它可能是startup.cs如何安排的顺序?
这是请求/响应
@rovdjuret的解决方法帮助了我,直到asp.net团队解决了它.这是我的控制器登录操作:
public IActionResult Login(string returnUrl = null) { if (_signInManager.IsSignedIn(User)) { // redirect to user profile page return RedirectToAction(nameof(HomeFileController.Index), "HomeFile"); } else { // clear Identity.External cookie if (Request.Cookies["Identity.External"] != null) { Response.Cookies.Delete("Identity.External"); } return View(new LoginViewModel{ ReturnUrl = returnUrl, RememberMe = true }); } }
更新:在最新版本(截至2017年5月),cookie的前缀为".AspNetCore.".所以cookie名称应为" .AspNetCore.Identity.External "
我也遇到了同样的问题.我从这里使用IdentityServer4 QuickStart示例中的代码
app.UseGoogleAuthentication(new GoogleOptions { AuthenticationScheme = "Google", DisplayName = "Google", SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme, ClientId = "xxx.apps.googleusercontent.com", ClientSecret = "xxxx-Xxxxxxx" });
我不得不将代码更改为以下内容来解决问题.
var CookieScheme= app.ApplicationServices.GetRequiredService>().Value.Cookies.ExternalCookieAuthenticationScheme; app.UseGoogleAuthentication(new GoogleOptions { AuthenticationScheme = "Google", DisplayName = "Google", SignInScheme = CookieScheme, ClientId = "xxx.apps.googleusercontent.com", ClientSecret = "xxxx-Xxxxxxx" });
IdentityServerConstants.ExternalAUthenticationScheme
我不得不使用常量"外部" 来获取用于从应用程序使用的当前身份系统的cookie选项中识别外部身份验证cookie的方案.这就是为我解决问题的原因.