我正在尝试在我的 mvc 4 web api 中使用授权属性,但它不起作用。我知道我错过了一些东西,但我找不到。
[HttpGet]
[Authorize]
public JArray getDistance(string origin, string destination)
{
some code..
}
我的 web.config 文件是-
<authentication mode="Forms">
<forms loginUrl="Login.aspx" requireSSL="true" cookieless="UseCookies" name="authcookie" protection="All" slidingExpiration="false">
</forms>
</authentication>
我的表单身份验证 cookie 是-
FormsAuthenticationTicket token = new FormsAuthenticationTicket(
1,
UserName,
DateTime.Now,
DateTime.Now.AddHours(24),
false,
"user Token"
);
string EncToken = FormsAuthentication.Encrypt(token);
HttpCookie cookie = new HttpCookie("authcookie", EncToken);
cookie.HttpOnly = true;
HttpContext.Current.Response.Cookies.Set(cookie);