尝试解密FormsAuthentication凭单始终无法验证数据
我正在使用新的webapi。尝试解密FormsAuthentication凭单始终无法验证数据
现在我不知道我是否正确地做到了这一点,但我试图设置我的API以返回HttpResponseMessages头中的身份验证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应用程序,我调用服务,并从我设置在标题中的数据值api控制器。
string data = response.Headers["result"].ToString();
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(data);
每次我尝试运行FormsAuthentication.Decrypt我不断收到一个错误
无法验证数据。
我认为,当api加密数据时,它会使用某种网站不知道的密钥。我对吗?
有人可以帮忙吗?
谢谢
我认为它是由于当API加密它使用关键的是,网站不知道的某种 数据。我对吗?
FormsAuthentication.Encrypt和Decrypt方法使用machine key。因此,请确保您为Web API Web应用程序和使用的ASP.NET MVC应用程序配置了相同的密钥。
您也可以查看following article,它说明了如何将OAuth 2.0与Web API一起使用。
任何想法如何抛出一个错误,以便我可以清除我的cookie并将用户从异常重定向到控制器?我的cookies持续存在,因此我陷入了重定向循环。当我更新密钥时会发生这种情况。 Web API返回401,但MVC重定向到登录页面,并且登录页面请求再次发回cookie,引发未经授权的响应。陷入恶性循环! – Shouvik 2014-11-14 16:02:58
此行中'data'的值是什么id string = response.Headers [“result”]。ToString();'? – Aliostad 2012-04-12 08:40:34