C# 错误-SignInResponse消息只能在当前web应用程序-MVC 2.0应用程序中重定向

C# 错误-SignInResponse消息只能在当前web应用程序-MVC 2.0应用程序中重定向,c#,asp.net-mvc,federated-identity,adfs2.0,sts-securitytokenservice,C#,Asp.net Mvc,Federated Identity,Adfs2.0,Sts Securitytokenservice,我有一个情况,我们有一个MVC2应用程序(我用一个基本的MVC2应用程序尝试了这个,没有任何额外的东西,仍然是相同的问题),我使用adfs 2来验证我的用户 所以。。 现在我进入我的应用程序,我得到了以下信息。。 ID3206:SignInResponse消息只能在当前web应用程序内重定向:“/[app]”是不允许的。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:Microsoft.IdentityModel.Pr

我有一个情况,我们有一个MVC2应用程序(我用一个基本的MVC2应用程序尝试了这个,没有任何额外的东西,仍然是相同的问题),我使用adfs 2来验证我的用户

所以。。 现在我进入我的应用程序,我得到了以下信息。。 ID3206:SignInResponse消息只能在当前web应用程序内重定向:“/[app]”是不允许的。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:Microsoft.IdentityModel.Protocols.FederationException:ID3206:SignInResponse消息只能在当前web应用程序中重定向:“/[app]”是不允许的。

我读过大多数关于这方面的博客,并在其中一个上发表了文章

    <federatedAuthentication>
            <wsFederation passiveRedirectEnabled="true" issuer="https://auth.[domain]/adfs/ls/" realm="https://[development domain]/[app]/" requireHttps="true" />
            <cookieHandler requireSsl="true" />
          </federatedAuthentication>
<audienceUris>
    <add value="https://[development domain]/[app]/" />
  </audienceUris>

  • 我在领域和audienceUris上有一个尾随斜杠
  • 我已将他建议的内容添加到应用程序_BeginRequest中–然后我将代码复制到[development domain],因为证书就在那里。。它只是陷入了一个无限循环
  • 我还检查了我在日内瓦服务器上的依赖方。。标识符和端点(POST)都是https://[development domain]/[app]/-同样带有尾随斜杠
  • 我认为这是一个问题,因为它是一个MVC应用程序,我已经创建了许多声明感知网站,并在default.aspx页面上获得了我的声明等。我的想法是,MVC应用程序涉及的路由在某种程度上是发回错误的

    任何帮助都是值得的,因为我现在静静地看了一会儿,都没有用


    这件事我一直很生气。我也在配置中指定了尾随斜杠。事实证明,在我的例子中,在浏览器中用一个尾随斜杠导航到我的应用程序,如下所示:

    将起作用,而

    不会


    如果我能找出更多的原因,我将添加更多的背景知识来解释为什么会发生这种情况。

    我在向我的web应用程序添加STS引用时遇到了这个问题,默认情况下,该应用程序在动态端口上的虚拟服务器下运行。我将其更改为在IIS中运行(与虚拟web服务器一样,除非在IIS/IIS Express中运行,否则不会重定向到STS),并通过web.config手动编辑以更改Microsoft.IdentityModel配置下的访问群体URI


    当我查看FederationMetadata.xml时,它仍然引用旧位置(带有动态端口)。我通过再次添加STS引用刷新了我的STS引用,它成功了。

    这段代码解决了这个问题(将它放在global.asax中):

    编辑:


    要检查的另一件事是Web.config中的microsoft.identityModel中的federationAuthentication/wsFederation元素。验证颁发者和域是否正确。

    我覆盖
    WSFederationAuthenticationModule
    子类上的
    重定向到IdentityProvider
    。这在重定向到STS之前只发生一次。您必须告诉配置文件使用此类
    FixedWSFederationAuthenticationModule
    而不是defualt
    WSFederationAuthenticationModule

    public class FixedWSFederationAuthenticationModule : WSFederationAuthenticationModule
    {
        public override void RedirectToIdentityProvider(string uniqueId, string returnUrl, bool persist)
        {
            //This corrects WIF error ID3206 "A SignInResponse message may only redirect within the current web application:"
            //First Check if the request url doesn't end with a "/"
            if (!returnUrl.EndsWith("/"))
            {
                //Compare if Request Url +"/" is equal to the Realm, so only root access is corrected
                //https://localhost/AppName plus "/" is equal to https://localhost/AppName/
                //This is to avoid MVC urls
                if (String.Compare(System.Web.HttpContext.Current.Request.Url.AbsoluteUri + "/", base.Realm, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    //Add the trailing slash
                    returnUrl += "/";
                }
            }
            base.RedirectToIdentityProvider(uniqueId, returnUrl, persist);
        }
    }
    

    我正在使用WIF的表单身份验证。forms auth模块将未经授权的请求重定向到正确的控制器,并将最初请求的URL存储在
    ReturnUrl
    参数中,因此我通过重写
    GetReturnUrlFromResponse
    方法解决了此错误

    /// <summary>
    /// Provides a workaround for a bug in the standard authentication module.
    /// </summary>
    /// <remarks>
    /// This class corrects WIF error ID3206 "A SignInResponse message may only
    /// redirect within the current web application..."
    /// WSFAM produces the error when the ReturnUrl is the root of the web application,
    /// but doesn't have a trailing slash. For instance, "/app" is considered incorrect
    /// by WSFAM whereas "/app/" is correct.
    /// </remarks>
    public class FixedWsFederationAuthenticationModule : System.IdentityModel.Services.WSFederationAuthenticationModule
    {
        /// <summary>
        /// Extracts the URL of the page that was originally requested from
        /// the sign-in response.
        /// </summary>
        /// <returns>
        /// The URL of the page that was originally requested by the client.
        /// This is the URL (at the relying party) to which the client should
        /// be redirected following successful sign-in.
        /// </returns>
        /// <param name="request">
        /// The HTTP request that contains a form POST, which contains the
        /// WS-Federation sign-in response message.
        /// </param>
        protected override string GetReturnUrlFromResponse(HttpRequestBase request)
        {
            string returnUrl = base.GetReturnUrlFromResponse(request);
    
            // First Check if the request url doesn't end with a "/"
            if (!string.IsNullOrEmpty(returnUrl) && !returnUrl.EndsWith("/"))
            {
                // Compare if (return Url +"/") is equal to the Realm path,
                // so only root access is corrected.
                // /AppName plus "/" is equal to /AppName/
                // This is to avoid MVC urls.
                if (string.Compare(
                    returnUrl + "/",
                    new Uri(Realm).LocalPath,
                    StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    // Add the trailing slash.
                    returnUrl += "/";
                }
            }
    
            return returnUrl;
        }
    }
    

    我对MVC4也有同样的问题。下面是web.config的语法:是否有理由子类化而不是放置
    WSFederationAuthenticationModule\u RedirectingToIdentityProvider
    RedirectingToIdentityProviderEventArgs
    允许您根据我的判断修改
    SignInRequestMessage
    。如果请求URL包含参数和/或域不是请求URL的子集或与请求URL不相同,则该代码将失败。这两种情况都是很有可能的。通过这种方法,我最终在signi上的IP-STS和RP之间进行了一些奇怪的无休止的重定向。我认为当时发生了一些其他事情,但我不能告诉你。这对我们来说是有效的,也是最容易实现的。我们的情况是,我们在一个网站中有多个虚拟应用程序,因此每个应用程序都作为登录页面,位于其自己的“文件夹”中,上述问题得到了解决。
    /// <summary>
    /// Provides a workaround for a bug in the standard authentication module.
    /// </summary>
    /// <remarks>
    /// This class corrects WIF error ID3206 "A SignInResponse message may only
    /// redirect within the current web application..."
    /// WSFAM produces the error when the ReturnUrl is the root of the web application,
    /// but doesn't have a trailing slash. For instance, "/app" is considered incorrect
    /// by WSFAM whereas "/app/" is correct.
    /// </remarks>
    public class FixedWsFederationAuthenticationModule : System.IdentityModel.Services.WSFederationAuthenticationModule
    {
        /// <summary>
        /// Extracts the URL of the page that was originally requested from
        /// the sign-in response.
        /// </summary>
        /// <returns>
        /// The URL of the page that was originally requested by the client.
        /// This is the URL (at the relying party) to which the client should
        /// be redirected following successful sign-in.
        /// </returns>
        /// <param name="request">
        /// The HTTP request that contains a form POST, which contains the
        /// WS-Federation sign-in response message.
        /// </param>
        protected override string GetReturnUrlFromResponse(HttpRequestBase request)
        {
            string returnUrl = base.GetReturnUrlFromResponse(request);
    
            // First Check if the request url doesn't end with a "/"
            if (!string.IsNullOrEmpty(returnUrl) && !returnUrl.EndsWith("/"))
            {
                // Compare if (return Url +"/") is equal to the Realm path,
                // so only root access is corrected.
                // /AppName plus "/" is equal to /AppName/
                // This is to avoid MVC urls.
                if (string.Compare(
                    returnUrl + "/",
                    new Uri(Realm).LocalPath,
                    StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    // Add the trailing slash.
                    returnUrl += "/";
                }
            }
    
            return returnUrl;
        }
    }
    
    <add name="WSFederationAuthenticationModule" type="YOUR_NAMESPACE.FixedWsFederationAuthenticationModule, YOUR_ASSEMBLY" preCondition="managedHandler" />