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

36

我有一个情况,我们有一个MVC 2应用程序(我尝试了一个基本的MVC 2应用程序,没有任何额外的东西,仍然出现同样的问题),并且正在使用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>
  1. 我的领域和audienceUris末尾都带有斜杠。
  2. 我已经添加了他建议的内容到Application_BeginRequest中 - 然后将代码复制到[开发域],因为那里是证书所在的地方。然后它就陷入了无限循环。
  3. 我还检查了Geneva服务器上的Relying Party。标识符和Endpoints(POST)都是https://[开发域]/[app]/,同样带有斜杠。

我认为问题在于它是一个MVC应用程序,我创建了许多Claims Aware网站,并在default.aspx页面上获取了我的claims等信息。我的想法是,与MVC应用程序有关的路由以某种方式错误地发布它?

非常感谢您的任何帮助,因为我已经看了很长时间,但没有结果。

J


1
我在MVC4中也遇到了同样的问题。 - SGarratt
5个回答

36

我在这个问题上抓耳挠腮。我的配置文件中也有尾随斜杠指定。结果,我发现,在浏览器中输入带有尾随斜杠的应用程序路径,例如:

http://localhost/myapp/

会起作用,而不带尾随斜杠的路径则不会,如下所示:

http://localhost/myapp

如果我找到更多原因说明这种情况,我会添加一些更多的背景信息。


18

我在WSFederationAuthenticationModule的子类中重写了RedirectToIdentityProvider。这只会在重定向到STS之前发生一次。您需要告诉配置文件使用FixedWSFederationAuthenticationModule类,而不是默认的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);
    }
}

4
这是您的 web.config 语法:<add name="FixedWSFederationAuthenticationModule" type="MyNamespace.STSHelper.FixedWSFederationAuthenticationModule, MyNamespace" preCondition="managedHandler" />。 - ADH
有没有一些理由可以子类化而不是直接使用WSFederationAuthenticationModule_RedirectingToIdentityProvider?根据我所知, RedirectingToIdentityProviderEventArgs允许您改变SignInRequestMessage - ta.speot.is
1
如果请求URL包含参数和/或领域不是子集或与请求URL相同,则该代码将失败。这两种情况都很可能发生。 - Joergen Bech

10

这段代码可以解决这个问题(将其放置在global.asax中):

private void Application_BeginRequest(object sender, EventArgs e)
{
//  This corrects WIF error ID3206 "A SignInResponse message may only redirect within the current web application: '/NHP' is not allowed."
//  For whatever reason, accessing the site without a trailing slash causes this error.
if (String.Compare(Request.Path, Request.ApplicationPath, StringComparison.InvariantCultureIgnoreCase) == 0 && !(Request.Path.EndsWith("/")))
Response.Redirect(Request.Path + "/");
}

编辑:

另外一个需要检查的是 Web.config 文件中 microsoft.identityModel 部分中的 federationAuthentication/wsFederation 元素。请验证发行人(issuer)和领域(realm)是否正确。


1
使用这种方法,我最终遇到了一些奇怪的IP-STS和RP之间的无限重定向问题。 - Vladislav
我认为还有其他事情发生了,但我无法告诉你是什么。 - Mike Cheel
这对我们很有效,也是最容易实现的。我们的情况是,在一个网站中有几个虚拟应用程序,因此每个应用程序都有自己的“文件夹”内的登录页面,上述方法解决了这个问题。 - Action Dan

5

我正在使用WIF进行表单认证。表单认证模块将未授权的请求重定向到正确的控制器,并在ReturnUrl参数中存储最初请求的URL,因此我通过覆盖GetReturnUrlFromResponse方法来解决了这个bug。

/// <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;
    }
}

为了使用这个类,您需要在web.config中注册它。 将这个元素添加到 system.webServer/modules 部分中,更改适当的部分。
<add name="WSFederationAuthenticationModule" type="YOUR_NAMESPACE.FixedWsFederationAuthenticationModule, YOUR_ASSEMBLY" preCondition="managedHandler" />

2

当我向我的网络应用程序添加STS引用时,遇到了这个问题,该应用程序默认在动态端口的虚拟服务器下运行。我将其更改为在IIS中运行(因为对于虚拟网络服务器,除非您在IIS / IIS Express中运行它,否则不会重定向到STS),并手动编辑web.config以更改Microsoft.IdentityModel配置下的受众URI。

当我查看FederationMetadata.xml时,它仍然引用旧位置(具有动态端口)。我通过重新添加STS引用进行刷新,它就可以工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接