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

发布于 2024-10-31 17:36:12 字数 621 浏览 1 评论 0原文

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

我尝试了

  • FormsAuthentication.SetAuthCookie
  • FormsAuthentication.GetAuthCookie + Response.Cookies.Add
  • new FormsAuthenticationTicket(...) a la MSDN
  • HttpModule< /code> 与 Page

以及其他一些绝望的尝试。似乎什么都不起作用。这是怎么做到的?

I have implemented forms authentication with a .NET Membership Provider but I also want users to be able to login with Facebook. Once authenticated with Facebook, I want to automatically assign a .NET authentication token to the user. I have a HttpModule that is detecting the FB authentication but all my attempts to manually generate an authentication token have come up short.

I tried

  • FormsAuthentication.SetAuthCookie
  • FormsAuthentication.GetAuthCookie + Response.Cookies.Add
  • new FormsAuthenticationTicket(...) a la MSDN
  • In an HttpModule vs Page

Plus a few other desperate attempts. Nothing seems to work. How is this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安稳善良 2024-11-07 17:36:12

建议的方法是使用 WIF

Proposed way is using WIF

作死小能手 2024-11-07 17:36:12
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);
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);
一个人的旅程 2024-11-07 17:36:12

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

After you SetCookieAuth you need to do a redirect to give the HttpModule a chance to fire and set the HttpContext.User property.

不乱于心 2024-11-07 17:36:12

事实证明,其他人在解决方案中注册了另一个模块,该模块干扰了 HttpRequest 和身份验证部分。在我摆脱它之后,FormsAuthentication.SetAuthCookie(...) 工作正常。

感谢大家的帮助。

It turns out someone else had registered another module in the solution that was interfering with HttpRequest and the authentication pieces. After I got rid of that, FormsAuthentication.SetAuthCookie(...) worked fine.

Thanks everyone for the help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文