身份验证和自定义错误页面

时间:2014-03-14 18:51:59

标签: acs custom-error-pages

我有一个使用Azure ACS进行身份验证的站点,由ADFS支持。当事情进展顺利,人们做事情时,他们应该做得很好,但事情并非如此,所以我们一直在实施自定义错误页面。

问题是,它似乎没有捕获到身份验证错误,例如

ID3206: A SignInResponse message may only redirect within the current web application
Key not valid for use in specified state.

无论我在web.config中说什么,这些错误仍然会产生丑陋的黄色错误屏幕。它们显然是ASP.NET错误,而不是IIS错误,所以我的问题是我如何以及在哪里可以放置自定义错误页面以在“漂亮”中显示此类错误。因为在web.config中设置页面不起作用吗?

编辑:要清楚,我们已将ACS设置为使用错误页面,使用不同的错误页面启用customErrors,既不使用也不使用。

1 个答案:

答案 0 :(得分:0)

您必须在Web应用程序中的控制器上执行操作,该控制器接受来自ACS的POST并采用string类型的参数。您还必须在ACS中配置依赖方应用程序以指向该操作以查找错误。然后在动作代码中你可以这样做:

namespace ASPNETSimpleMVC.Controllers
{
public class ErrorController : Controller
{
    // Errors can be mapped to custom strings here.
    static Dictionary<string, string> ErrorCodeMapping = new Dictionary<string, string>();

    static ErrorController()
    {
        ErrorCodeMapping["ACS50019"] = "You chose to cancel log-in to the identity provider.";
        ErrorCodeMapping["ACS60001"] = "No output claims were generated. You may be unauthorized to visit this site.";
    }

    //
    // POST: /Error/
    //
    // If an error occurs during sign-in, ACS will post JSON-encoded errors to this endpoint.
    // This function displays the error details, mapping specific error codes to custom strings.
    [AcceptVerbs( HttpVerbs.Post )]
    public ActionResult Index( string ErrorDetails )
    {
        // The error details contain an array of errors with unique error codes to indicate what went wrong.
        // Additionally, the error details contain a suggested HTTP return code, trace ID, and timestamp, which may be useful for logging purposes.

        ErrorDetails parsedErrorDetails = new JavaScriptSerializer().Deserialize<ErrorDetails>( ErrorDetails );

        ViewData["ErrorMessage"] = String.Format( "An error occurred during sign-in to {0}. ", parsedErrorDetails.identityProvider );

        // Loop through all ACS errors, looking for ones that are mapped to custom strings.
        // When a mapped error is found, stop looking and append the custom string to the error message.
        foreach ( ErrorDetails.Error error in parsedErrorDetails.errors )
        {
            if ( ErrorCodeMapping.ContainsKey( error.errorCode ) )
            {
                ViewData["ErrorMessage"] += ErrorCodeMapping[error.errorCode];
                break;
            }
        }

        return View( "Error" );
    }
}
}

您可能还会发现this article有帮助。