gpt4 book ai didi

asp.net-mvc - 尝试解密 FormsAuthentication 票证始终无法验证数据

转载 作者:行者123 更新时间:2023-12-02 11:13:37 25 4
gpt4 key购买 nike

我正在使用新的 webapi。

现在我不知道我这样做是否正确,但我正在尝试设置我的 api 以在 HttpResponseMessages header 中返回身份验证 cookie,以便在另一个 mvc 应用程序上使用。

我正在使用 FormsAuthenticationTicket,因为我认为它是我需要使用的

  public HttpResponseMessage Get(LoginModel model)
{
if (model.UserName == "bob")
{
// if (Membership.ValidateUser(model.UserName, model.Password))
// {
var msg = new HttpResponseMessage(HttpStatusCode.OK);
var expires = DateTime.Now.AddMinutes(30);
var auth = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, expires,
model.RememberMe,"password",
FormsAuthentication.FormsCookiePath);
var cookie = new HttpCookie("user");
cookie.Value = FormsAuthentication.Encrypt(auth);
cookie.Domain = "localhost";
cookie.Expires = expires;
msg.Headers.Add("result",cookie.Value);
return msg;
// }
}
return new HttpResponseMessage(HttpStatusCode.Forbidden);
//else
//{
// return "The user name or password provided is incorrect.";
//}
}

现在,在我的 mvc 应用程序上的登录 Controller 中,我调用该服务并从我在 api Controller 中设置的 header 中获取数据值。

   string data = response.Headers["result"].ToString();
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(data);

每次我尝试运行 FormsAuthentication.Decrypt 时,我都会收到错误

Unable to validate data.

我认为这是由于 api 加密数据时使用了网站不知道的某种 key 。我说得对吗?

有人可以帮忙吗?

谢谢

最佳答案

I assume its due to when the api encrypts the data it uses some kind of key that the website doesn't know about. Am I right?

FormsAuthentication.Encrypt 和 Decrypt 方法使用 machine key 。因此,请确保您为 Web API Web 应用程序和使用的 ASP.NET MVC 应用程序配置了相同的 key 。

您还可以查看 following article其中说明了如何将 OAuth 2.0 与 Web API 结合使用。

关于asp.net-mvc - 尝试解密 FormsAuthentication 票证始终无法验证数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com