将基于声明的授权添加到MVC 3

时间:2011-05-13 21:16:34

标签: asp.net-mvc asp.net-mvc-3 wif claims-based-identity

我有一个MVC应用程序,我想添加基于声明的授权。在不久的将来,我们将使用ADFS2进行联合身份验证,但现在我们将在本地使用表单身份验证。

有没有人看过有关在没有外部身份提供商的情况下使用WIF的最佳方式的教程或博客文章?

我见过以下内容,但现在已经有一年了,我认为应该有一个更简单的解决方案:

http://geekswithblogs.net/shahed/archive/2010/02/05/137795.aspx

2 个答案:

答案 0 :(得分: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" />

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上部署。它也可以在配置中定义。

答案 1 :(得分:1)

WIF旨在使用STS,所以如果您不想这样做,那么您必须根据文章重新发明轮子。

当您转移到ADFS时,您几乎不得不重新编码所有内容。

或者,看看StarterSTS,这实现了您需要的相同类型的aspnetdb身份验证,但允许WIF执行繁重的工作。然后,当您迁移到ADFS时,您只需针对ADFS运行FedUtil,它将全部工作,无需任何重大编码更改。

(顺便说一句,有一个MVC版本 - 后来的实现 - here)。

相关问题