すぐにリダイレクトするのではなく、JavaScript を発行してリダイレクトを実行することにより、ハッシュ部分を保持できます。JavaScript コードは、window.location.hash を介してハッシュ部分にアクセスし、それを使用して ru を構築できます。
認証されていないユーザーを許可するようにページを構成する必要があります (WIF パッシブ認証が開始されないようにするため)。その後、ページ コードで認証されていないユーザーを処理できます。
アプリケーションの起動コードで FederatedAuthentication.WSFederationAuthenticationModule.RedirectingToIdentityProvider イベントをフックできます (例: Web フォームの Global.asax.cs)。
例 (Web フォーム):
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
FederatedAuthentication.WSFederationAuthenticationModule.RedirectingToIdentityProvider
+= this.RedirectToIdentityProviderViaJavaScript;
}
const string RedirectHtml =
@"<html>
<head>
<script type='text/javascript'>
function authenticate(url, utcTimeString) {{
var ru = window.location.pathname + (window.location.hash || '');
var wctx = 'rm=0&id=passive&ru=' + encodeURIComponent(ru) + '&wtc=' + encodeURIComponent(utcTimeString);
url += '&wctx=' + encodeURIComponent(wctx);
window.location = url;
}}
</script>
</head>
<body onload=""authenticate('{0}', '{1}');"">
</body>
</html>";
private void RedirectToIdentityProviderViaJavaScript(object sender, RedirectingToIdentityProviderEventArgs e)
{
var fam = FederatedAuthentication.WSFederationAuthenticationModule;
var msg = new SignInRequestMessage(new Uri(fam.Issuer), fam.Realm);
var stsUrl = msg.WriteQueryString();
var utcTime = WebPageRoutines.EncodeUtcTimeString(DateTime.Now);
var html = string.Format(RedirectHtml, WebPageRoutines.JavascriptEncode(stsUrl), WebPageRoutines.JavascriptEncode(utcTime));
Response.ClearContent();
Response.Write(html);
Response.Status = "200 OK";
Response.End();
}
}
混合できないことに注意してください。このアプローチでは # パーツのパラメーター。ru は STS リダイレクト (Thinktecture IdentityServer v2) を生き延びますが、WIF は STS からの POST 後の最終リダイレクトでそれを台無しにするようです。
? を配置します。# の後の部分。
http://www.somewebsite.com/page?param=1&other=2#hashbit
は次のようになります:
http://www.somewebsite.com/page#hashbit?param=1&other=2