【问题标题】:What is the proper way to implement Form Authentication along side Facebook/Other authentication in ASP.NET MVC在 ASP.NET MVC 中与 Facebook/其他身份验证一起实现表单身份验证的正确方法是什么
【发布时间】:2012-01-10 13:51:13
【问题描述】:

我正在 ASP.NET MVC 3 中构建一个应用程序,其中我已经实现了表单身份验证。

我还想为用户提供使用其 Facebook 帐户(以及未来可能的其他社交帐户)注册/登录的选项。

我正在使用C# Facebook SDK

我已经成功实现了 facebook 登录工作流程。现在我的问题是,如何处理混合表单和 Facebook Auth?我似乎找不到任何足够的例子来说明如何做到这一点。

关于我的 facebook 实现,我正在请求用户许可我将存储在我的数据库中的非过期 Auth Token。

【问题讨论】:

    标签: c# asp.net asp.net-mvc facebook facebook-c#-sdk


    【解决方案1】:

    为此,您将不得不这样做

    1. 创建一个实现 IIdentity 的自定义类 (CurrentIdentity)。为此类覆盖 .ToString() ,以便您拥有某种序列化的对象状态。我曾使用“|”分隔符。此字符串将存储在加密的 cookie 中。
    2. 创建会话 cookie

      public static HttpCookie AuthCookie(CurrentIdentity identity) {
         //Create the Authticket, store it in Cookie and redirect user back
         FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
                            identity.Name, DateTime.Now, DateTime.Now.AddHours(3), true, identity.ToString(), FormsAuthentication.FormsCookiePath);
         string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
         HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
         authCookie.Expires = authTicket.Expiration;
         return authCookie;
      }
      
    3. 将此 cookie 添加到响应中。

      HttpResponse res = HttpContext.Current.Response;
      res.Cookies.Add(Helper.AuthCookie(identity));
      res.Redirect(FormsAuthentication.GetRedirectUrl(identity.Name, true));
      res.End();
      
    4. 现在在 Gloabl.asax 中,在 *Application_AuthenticateRequest* 中读取此 cookie 并填充您的 CurrentIdentity 对象,类似这样。

      if (Request.IsAuthenticated == true) {
              // Extract the forms authentication cookie
              string cookieName = FormsAuthentication.FormsCookieName;
              HttpCookie authCookie = Context.Request.Cookies[cookieName];
      
              if (null == authCookie) {
                  // There is no authentication cookie.
                  return;
              }
              FormsAuthenticationTicket authTicket = null;
              try {
                  authTicket = FormsAuthentication.Decrypt(authCookie.Value);
              } catch (Exception) {
                  // Log exception details (omitted for simplicity)
                  return;
              }
      
              if (null == authTicket) {
                  // Cookie failed to decrypt.
                  return;
              }
              // When the ticket was created, the UserData property was assigned a
              // pipe delimited string of role names.
              string[] userInfo = authTicket.UserData.Split('|');
      
              // Create an Identity object
              FormsIdentity id = new FormsIdentity(authTicket);
              //Populate CurrentIdentity, from Serialized string
              CurrentIdentity currentIdentity = new CurrentIdentity(userInfo[0], userInfo[1], userInfo[2]);
              System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(currentIdentity, userInfo);
              Context.User = principal;
      }
      

    这应该可以解决您的问题。我在company's website 上实现了类似的东西。

    【讨论】:

    • 感谢您的深入回答。我将尝试对所有内容进行分类并让某些东西发挥作用。
    【解决方案2】:

    将 facebook 身份验证与您的表单身份验证合并。 当用户使用表单身份验证登录时,您会创建 FormsAuthenticationTicket,当从 facebook 登录时也会创建 FormsAuthenticationTicket

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      相关资源
      最近更新 更多