chi*_*ef7 16 asp.net-mvc claims-based-identity wif asp.net-mvc-3
我有一个MVC应用程序,我想添加基于声明的授权.在不久的将来,我们将使用ADFS2进行联合身份验证,但现在我们将在本地使用表单身份验证.
有没有人看过关于在没有外部身份提供者的情况下使用WIF的最佳方式的教程或博客文章?
我见过以下但现在已经有一年了,我认为应该有一个更简单的解决方案:
http://geekswithblogs.net/shahed/archive/2010/02/05/137795.aspx
Eug*_*ace 21
您可以在没有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" />
Run Code Online (Sandbox Code Playgroud)
2-无论您在何处对用户进行身份验证,请创建ClaimsPrincipal,添加所有必需的声明,然后创建SessionSecurityToken.这是MVC创建的AccountController中的LogOn Action :
[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);
}
Run Code Online (Sandbox Code Playgroud)
我刚刚添加了所需的行,并将其他所有行保持不变.因此可能需要进行一些重构.
从那以后,您的应用程序现在将收到ClaimsPrincipal.全部由WIF自动处理.
该CookieHandler.RequiresSsl =假只是因为它是一个dev的机器,我不会在IIS上部署.它也可以在配置中定义.
| 归档时间: |
|
| 查看次数: |
10103 次 |
| 最近记录: |