C# 在.NET中自动分配身份验证令牌

C# 在.NET中自动分配身份验证令牌,c#,authentication,asp.net-membership,forms-authentication,httpmodule,C#,Authentication,Asp.net Membership,Forms Authentication,Httpmodule,我已经实现了与.NET成员资格提供商的表单身份验证,但我也希望用户能够使用Facebook登录。使用Facebook进行身份验证后,我想自动为用户分配一个.NET身份验证令牌。我有一个检测FB身份验证的HttpModule,但我所有手动生成身份验证令牌的尝试都失败了 我试过了 FormsAuthentication.SetAuthCookie FormsAuthentication.GetAuthCookie+Response.Cookies.Add 新表单身份证(…)a la 在HttpMo

我已经实现了与.NET成员资格提供商的表单身份验证,但我也希望用户能够使用Facebook登录。使用Facebook进行身份验证后,我想自动为用户分配一个.NET身份验证令牌。我有一个检测FB身份验证的
HttpModule
,但我所有手动生成身份验证令牌的尝试都失败了

我试过了

  • FormsAuthentication.SetAuthCookie
  • FormsAuthentication.GetAuthCookie
    +
    Response.Cookies.Add
  • 新表单身份证(…)
    a la
  • HttpModule
    vs
    Page

再加上其他一些绝望的尝试。似乎什么都不管用。这是如何做到的?

建议的方法正在使用

建议的方法正在使用

在您
设置CookieAuth
之后,您需要执行重定向,以使HttpModule有机会启动并设置
HttpContext.User
属性

SetCookieAuth
之后,您需要执行重定向,以使HttpModule有机会启动并设置
HttpContext.User
属性

结果是,其他人在解决方案中注册了另一个模块,该模块干扰了
HttpRequest
和身份验证块。在我摆脱了它之后,
FormsAuthentication.SetAuthCookie(…)
工作得很好


感谢大家的帮助。

结果发现,其他人在解决方案中注册了另一个模块,该模块干扰了
HttpRequest
和身份验证块。在我摆脱了它之后,
FormsAuthentication.SetAuthCookie(…)
工作得很好


感谢大家的帮助。

WIF将身份的来源和消费者分开,并提供简单的API从任何来源创建用户令牌。听起来非常强大、适当和复杂:/WIF的使用体验如何?插入并开始使用是否容易?几年前,我为多个标识源实现了类似WIF的解决方案。如今,WIF已在SharePoint和Azure等主要Microsoft产品中使用。WIF比实现灵活的多身份解决方案容易得多。WIF将身份的来源和使用者分开,并提供简单的API从任何来源创建用户令牌。听起来非常强大、适当和复杂:/WIF的使用体验如何?插入并开始使用是否容易?几年前,我为多个标识源实现了类似WIF的解决方案。如今,WIF已在SharePoint和Azure等主要Microsoft产品中使用。WIF比实现灵活的多身份解决方案要容易得多。我已经尝试过了,但它似乎不起作用。Will
Request.IsAuthenticated==true
使用此方法吗?我已经尝试过此方法,但似乎不起作用。是否将使用此方法
Request.IsAuthenticated==true
FormsAuthentication.Initialize();
// Create a new ticket used for authentication
FormsAuthentication.SetAuthCookie(UserName.Text, false);
// Create a new ticket used for authentication
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
   1, // Ticket version
   UserName.Text, // Username associated with ticket
   DateTime.Now, // Date/time issued
   DateTime.Now.AddMinutes(30), // Date/time to expire
   false, // "true" for a persistent user cookie
   "Admin", // User-data, in this case the roles
   FormsAuthentication.FormsCookiePath);// Path cookie valid for

// Encrypt the cookie using the machine key for secure transport
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
   FormsAuthentication.FormsCookieName, // Name of auth cookie
   hash); // Hashed ticket

// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

// Add the cookie to the list for outgoing response
Response.Cookies.Add(cookie);