STS なしで MVC で WIF を使用できます。
デフォルトの MVC2 テンプレートを使用しましたが、MVC 3 でも動作するはずです。
必要がある:
1- WIF のSessionAuthenticationModule (web.config)をプラグインします。
< add name="SessionAuthenticationModule" type="Microsoft.IdentityModel.Web.SessionAuthenticationModule, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
2- ユーザーを認証する場所で、ClaimsPrincipalを作成し、必要なすべてのクレームを追加してから、SessionSecurityTokenを作成します。これは、MVC によって作成されたAccountControllerのLogOnアクションです。
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
var cp = new ClaimsPrincipal();
cp.Identities.Add(new ClaimsIdentity());
IClaimsIdentity ci = (cp.Identity as IClaimsIdentity);
ci.Claims.Add(new Claim(ClaimTypes.Name, model.UserName));
SessionSecurityToken sst = FederatedAuthentication
.SessionAuthenticationModule
.CreateSessionSecurityToken(cp,
"MVC Test",
DateTime.
UtcNow,
DateTime.
UtcNow.
AddHours
(1),
true);
FederatedAuthentication.SessionAuthenticationModule.CookieHandler.RequireSsl = false;
FederatedAuthentication.SessionAuthenticationModule.AuthenticateSessionSecurityToken(sst, true);
//FormsService.SignIn(model.UserName, model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
必要な行を追加しただけで、他のすべては同じままにしました。そのため、いくつかのリファクタリングが必要になる場合があります。
それ以降、アプリはClaimsPrincipalを受け取ります。すべてWIFによって自動的に処理されます。
CookieHandler.RequiresSsl = falseは、それが開発マシンであり、IIS に展開していないためです。構成でも定義できます。