16

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

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

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

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

4

2 回答 2

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 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);
        }

我只是添加了所需的行,其他一切都保持不变。所以可能需要进行一些重构。

从那里开始,您的应用程序现在将收到ClaimsPrincipal。全部由 WIF 自动处理。

CookieHandler.RequiresSsl = false只是因为它是开发机器,我没有在 IIS 上部署。它也可以在配置中定义。

于 2011-05-20T03:56:42.507 回答
1

WIF 旨在使用 STS,因此如果您不想这样做,那么您基本上必须按照文章重新发明轮子。

当您迁移到 ADFS 时,您几乎必须重新编码所有内容。

或者,看看StarterSTS,它实现了您需要的相同类型的 aspnetdb 身份验证,但允许 WIF 完成繁重的工作。然后,当您迁移到 ADFS 时,您只需针对 ADFS 运行 FedUtil,它就可以在没有任何重大编码更改的情况下运行。

(顺便说一句,这里有一个 MVC 版本 - 稍后的实现)。

于 2011-05-15T19:46:50.983 回答