Ответ 1
Вы можете использовать WIF в MVC без STS.
Я использовал шаблон MVC2 по умолчанию, но он также должен работать с MVC 3.
Вам необходимо:
1- Plug 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. Это действие LogOn в AccountController, созданное MVC:
[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 - это только потому, что это dev, и я не развертываю в IIS. Его также можно определить в конфигурации.