简体   繁体   English

ASP.net C# 网络表单 ADFS 出现 CORS 错误

[英]ASP.net C# webform ADFS hitting CORS error

I keep hitting CORS error when i want to redirect to ADFS server in webform.当我想重定向到 webform 中的 ADFS 服务器时,我一直遇到 CORS 错误。 Below are the error that i hit:以下是我遇到的错误:

CORS错误

I tried few method as mention in below link: CORS endpoints on asp.net Webforms [WebMethod] endpoints我尝试了以下链接中提到的几种方法: CORS endpoints on asp.net Webforms [WebMethod] endpoints

However, nothing is working.但是,没有任何效果。 Not sure if I missed anything in the ADFS setting?不确定我是否遗漏了 ADFS 设置中的任何内容? In my other project that is using MVC, it is working fine.在我使用 MVC 的其他项目中,它运行良好。 Just the webform keep hitting this error.只是网络表单不断出现此错误。

Login.aspx.cs登录.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外部登录.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);
        }
    }
}

I have found the solution.我找到了解决方案。

Solution:解决方案:

https://learn.microsoft.com/en-us/windows-server/identity/ad-fs/operations/customize-http-security-headers-ad-fs https://learn.microsoft.com/en-us/windows-server/identity/ad-fs/operations/customize-http-security-headers-ad-fs

Run the PowerShell script to enable it运行 PowerShell 脚本启用它

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM