【发布时间】:2021-05-20 04:37:30
【问题描述】:
当我想在 webform 中重定向到 ADFS 服务器时,我一直遇到 CORS 错误。 以下是我遇到的错误:
我尝试了以下链接中提到的几种方法: CORS endpoints on asp.net Webforms [WebMethod] endpoints
但是,没有任何效果。不确定我是否错过了 ADFS 设置中的任何内容? 在我使用 MVC 的另一个项目中,它运行良好。只是网络表单不断出现此错误。
登录.aspx.cs
protected void LoginSSO(object sender, EventArgs e)
{
Response.AppendHeader("Access-Control-Allow-Origin", "*");
Response.AppendHeader("Access-Control-Allow-Methods", "*");
ExternalLogin bUsr = new ExternalLogin();
HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
var translator = new ActionResultTranslator(contextWrapper);
translator.Execute(bUsr.ExternalLoginADFS("ExternalLoginCallback.aspx"));
}
ExternalLogin.aspx.cs
public partial class ExternalLogin : System.Web.UI.Page
{
private const string XsrfKey = "XsrfId";
public string RedirectUri { get; private set; }
[AllowAnonymous]
public ActionResult ExternalLoginADFS(string returnUrl)
{
return new ChallengeResult(WsFederationAuthenticationDefaults.AuthenticationType, "ExternalLoginCallback.aspx");
}
[HttpPost]
[AllowAnonymous]
public ActionResult ExternalLoginADFS(string provider, string returnUrl)
{
return new ChallengeResult(provider, "ExternalLoginCallback.aspx");
}
internal class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUri)
: this(provider, redirectUri, null)
{
}
public ChallengeResult(string provider, string redirectUri, string userId)
{
LoginProvider = provider;
RedirectUri = redirectUri;
UserId = userId;
}
public string LoginProvider { get; set; }
public string RedirectUri { get; set; }
public string UserId { get; set; }
public Task<ActionResult> Task { get; }
public class ActionResultTranslator
{
HttpContextBase _context;
public ActionResultTranslator(HttpContextBase context)
{
_context = context;
}
[HttpGet]
public void Execute(ActionResult actionResult)
{
ControllerContext fakeContext = new ControllerContext();
fakeContext.HttpContext = _context;
actionResult.ExecuteResult(fakeContext);
}
}
[HttpGet]
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XsrfKey] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
}
}
【问题讨论】:
-
您能分享一下您如何从 Web 表单重定向到 ADFS 服务器的代码吗?
-
我已经用代码编辑了帖子。
-
我发现有些浏览器(可能是 Chrome?Firefox?我不记得了)不喜欢 * 作为“Access-Control-Allow-Origin”值。
-
刚刚从 ADFS 方面,什么版本? 2016 年没有 CORS 支持 - docs.microsoft.com/en-us/windows-server/identity/ad-fs/…
-
@rbrayb 你是对的。运行powershell脚本后,确实解决了问题