【问题标题】:Adding Claims-based authorization to MVC 3向 MVC 3 添加基于声明的授权
【发布时间】:2011-08-25 06:23:53
【问题描述】:

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    您可以在没有 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 上。也可以在配置中定义。

    【讨论】:

    • 那么您将如何添加另一个身份提供者?像谷歌或 adfs ?
    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 2014-03-15
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 2014-05-01
      • 1970-01-01
      相关资源
      最近更新 更多