16

クレーム ベースの承認を追加したい MVC アプリがあります。近い将来、フェデレーション ID に ADFS2 を使用する予定ですが、今のところはフォーム認証をローカルで使用します。

外部 ID プロバイダーなしで WIF を使用する最良の方法についてのチュートリアルまたはブログ投稿を見た人はいますか?

私は次のことを見てきましたが、今では1年前なので、もっと簡単な解決策があるはずです:

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アクションです。

 [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バージョンがあります-後の実装- here)。

于 2011-05-15T19:46:50.983 に答える