簡體   English   中英

授權屬性不起作用

[英]Authorize Attribute is not working

我試圖在我的MVC 4 Web API中使用authorize屬性,但是它不起作用。 我知道我想念一些東西,但找不到。

[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);

對於Web API,請確保您正在使用

System.Web.Http命名空間

System.Web.Mvc

在AFAIK中,您可以[授權]控制器的動作。 但我不知道嘗試任何其他方法。 需要考慮的幾件事,請檢查您的Cookie在登錄和注銷期間是否已正確清除或存儲。 您可以編寫自定義授權過濾器。 檢查這個

祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM