asp.net-mvc – 向MVC 3添加基于声明的授权
|
我有一个MVC应用程序,我想添加基于声明的授权。在不久的将来,我们将使用ADFS2进行联合身份验证,但现在我们将在本地使用表单身份验证。 有没有人看到一个教程或博客文章关于没有外部身份提供商使用WIF的最佳方式? 我已经看到以下,但现在是一岁,我认为应该有一个更容易的解决方案: http://geekswithblogs.net/shahed/archive/2010/02/05/137795.aspx 解决方法您可以在没有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-无论何时验证用户,创建ClaimPrincipal,添加所有必需的声明,然后创建一个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);
}
我只是添加了所需的行,并保持一切都一样。因此可能需要重构。 从那时起,您的应用程式将会收到ClaimPrincipal。全部由WIF自动处理。 CookieHandler.RequiresSsl = false只是因为它是一个开机,我不在IIS上部署。它也可以在配置中定义。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-4 – 会话到期后重定向到特定页面(MVC4)
- 本地化 – 了解MVC6 RC1中的资源文件
- asp.net-mvc – 使用Razor视图引擎 – 如何格式化十进制值以
- asp.net – 如何停止调用500错误页面创建的500 .net错误
- asp.net-mvc – 在MVC Action中将SSRS报告导出为PDF
- asp.net – 超过了JavaScriptSerializer.MaxJsonLength.处理
- ASP.Net C#AssemblyInfo版本不起作用
- dependency-injection – 如何使用unity注入ApplicationUse
- asp.net-mvc-4 – ASP.NET MVC 4自定义权限属性 – 如何将未
- 将linkbutton设置为asp.net中asp:panel的默认按钮[复制]
- asp.net-identity-2 – 没有实体框架的ASP.NET I
- asp.net-mvc – 如何使用Razor ASp.NET MVC连接H
- asp.net – 转义HTML实体并避免WebForm标签中的H
- asp.net-mvc – 使用jQuery ASP.NET MVC自动保存
- asp.net-mvc – ASP.NET MVC 3 RC和Azure?
- 在ASP.NET应用程序的global.asax中处理Applicati
- ef-code-first – 如何使用LocalDB和EF,而不使用
- asp.net – IIS 7.5不加载静态html页面
- asp.net – 为什么当试图保存更改时,GridView行“
- asp.net-mvc – 仅使用Entity Framework更新已修
