Tôi đang sử dụng các nhà cung cấp xác thực bên ngoài của OWIN trong dự án ASP.Net MVC 5/WebApi 2 của tôi và tôi đã gặp phải sự cố lạ.Lần đăng nhập bên ngoài đầu tiên chuyển hướng trở lại hành động đăng nhập, lần thứ hai hoạt động
Quy trình đăng nhập giống hệt như ở đây trên SO. Người sử dụng truy cập các trang đăng nhập, chọn một nhà cung cấp và sẽ được ghi lại trong vấn đề của tôi là nhấp chuột đầu tiên vào một nhà cung cấp chuyển hướng trở lại với cùng trang đăng nhập:.
http://localhost:57291/Account/Login?ReturnUrl=%2fAccount%2fExternalLogin
này sẽ có ý nghĩa nếu hành động ExternalLogin sẽ thiếu thuộc tính AllowAnonymous.
Khi người dùng nhấp vào lần thứ hai mọi thứ hoạt động.
Tôi cũng đã thử rằng với các trình duyệt khác nhau và sự cố nhất quán trên Chrome, IE11 và Firefox.
Login.cshtml:
@using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = ViewBag.ReturnUrl }))
{
<fieldset>
<legend>@Strings.ExternalAuthenticationProvidersDescription</legend>
<p>
@foreach (var p in Model.ExternalAuthenticationProviders)
{
<button type="submit" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.Caption</button>
}
</p>
</fieldset>
}
AccountController.cs
public class AccountController : Controller
{
...
[AllowAnonymous]
[HttpPost]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new
{
loginProvider = provider,
ReturnUrl = returnUrl
}));
}
...
}
ChallengeResult.cs:
public class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUrl)
{
LoginProvider = provider;
RedirectUrl = redirectUrl;
}
public string LoginProvider { get; set; }
public string RedirectUrl { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties
{
RedirectUri = RedirectUrl
}, LoginProvider);
}
}
FilterConfig.cs
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
// make all api controllers secure by default
filters.Add(new AuthorizeAttribute());
}
}
điều này thực sự cố định nó cho tôi! cảm ơn –