2009-07-27 16 views
5

MVC Storefrontから次のコードを使用してMVCでOpenIdをテストしています。 ロールを使用してユーザーのユーザー名をテーブルに保存できるように、ASP.Netメンバーシップとの統合方法を教えてください。私はSOも同様のものを使用していると信じています。OpenIdとASP.Netを統合する方法MVCのメンバーシップ

public ActionResult OpenIdLogin() 
    { 
     string returnUrl = VirtualPathUtility.ToAbsolute("~/"); 
     var openid = new OpenIdRelyingParty(); 
     var response = openid.GetResponse(); 
     if (response == null) 
     { 
      // Stage 2: user submitting Identifier 
      Identifier id; 
      if (Identifier.TryParse(Request["openid_identifier"], out id)) 
      { 
       try 
       { 
        IAuthenticationRequest req = openid.CreateRequest(Request["openid_identifier"]); 

        var fetch = new FetchRequest(); 
        //ask for more info - the email address 
        var item = new AttributeRequest(WellKnownAttributes.Contact.Email); 
        item.IsRequired = true; 
        fetch.Attributes.Add(item); 
        req.AddExtension(fetch); 

        return req.RedirectingResponse.AsActionResult(); 
       } 
       catch (ProtocolException ex) 
       { 
        ViewData["Message"] = ex.Message; 
        return View("Logon"); 
       } 
      } 
      else 
      { 
       ViewData["Message"] = "Invalid identifier"; 
       return View("Logon"); 
      } 
     } 
     else 
     { 
      // Stage 3: OpenID Provider sending assertion response 
      switch (response.Status) 
      { 
       case AuthenticationStatus.Authenticated: 

        var fetch = response.GetExtension<FetchResponse>(); 
        string name = response.FriendlyIdentifierForDisplay; 
        if (fetch != null) 
        { 
         IList<string> emailAddresses = fetch.Attributes[WellKnownAttributes.Contact.Email].Values; 
         string email = emailAddresses.Count > 0 ? emailAddresses[0] : null; 
         //don't show the email - it's creepy. Just use the name of the email 
         name = email.Substring(0, email.IndexOf('@')); 
        } 
        else 
        { 

         name = name.Substring(0, name.IndexOf('.')); 
        } 

        //FormsAuthentication.SetAuthCookie(name, false); 
        SetCookies(name, name); 
        AuthAndRedirect(name, name); 

        if (!string.IsNullOrEmpty(returnUrl)) 
        { 
         return Redirect(returnUrl); 
        } 
        else 
        { 
         return RedirectToAction("Index", "Home"); 
        } 
       case AuthenticationStatus.Canceled: 
        ViewData["Message"] = "Canceled at provider"; 
        return View("Logon"); 
       case AuthenticationStatus.Failed: 
        ViewData["Message"] = response.Exception.Message; 
        return View("Logon"); 
      } 
     } 
     return new EmptyResult(); 

    } 

    ActionResult AuthAndRedirect(string userName, string friendlyName) 
    { 
     string returnUrl = Request["ReturnUrl"]; 
     SetCookies(userName, friendlyName); 

     if (!String.IsNullOrEmpty(returnUrl)) 
     { 
      return Redirect(returnUrl); 
     } 
     else 
     { 
      return RedirectToAction("Index", "Home"); 
     } 
    } 

答えて

6

あなたのようないくつかの質問がすでにStackOverflowにあります。 This oneは特に類似しているようです。

あなたのサイトにすでにメンバーシッププロバイダを使用していて、OpenIDを追加しているだけの場合は、今すぐメンバーシップに悩まされていると思います。あなたのために働くことができる準会員制のプロバイダです。

しかし、新しいサイトを作成していて、言われたように「ロールを使用して自分のテーブルのユーザー名を保存する」必要がある場合は、ASP.NETメンバーシップをまったく使用しないでください。それはそれほど価値がありません!それはOpenIDのパスワードのないパラダイムに合っておらず、他の何よりも悲しみが増えます。あなたがデータベースへのアクセスを少しでも心配していなければ、そうしてください。 FormsAuthentication.RedirectFromLoginPageまたはFormsAuthentication.SetAuthCookieコールを発行し、ユーザーが入力したロールを渡すだけで、ロールの動作を簡単に取得できます。

+0

私は新しい実装で遊んでいるので、特にASP.NETメンバーシップとは結婚していません。あなたが言及した方法でそれを行う方法のサンプルコードを提案できますか? – Picflight

+0

サンプルコードでは "私も"ここに残します。あなたは何か知っていますか? –

+0

DotNetOpenAuthに同梱されているすべてのサンプルおよびプロジェクトテンプレートは、前述の方法で行います。 –

1

オープンIDプロバイダは、ユーザーに関するデータを返します。特定のトークンを要求/要求しない場合は、ユーザーの表示名とID URLだけが表示されます。

あなたが使用しているオープンIDライブラリによっては、FirstName LastName、DOB(本当に気にしていた場合)などのトークンを要求できます。また、ユーザーが選択したIDについて情報を提供した場合、君は。

これを使用して、メンバーシップシステムで新しいユーザーを作成できます。メンバーシップAPIの要件を克服するには、おそらくダミーのパスワードを与える必要があります。

ログインを確認するには、ユーザー名&のパスワードと身元確認URLを受け取る1つのフォームを入力します。オープンIDを使用してユーザーを検証した後、メンバーシップAPIでユーザー名(ID URL)でユーザーを検索してみてください。存在しない場合は作成してください。

関連する問題