0
我想保存在一個數據庫(日期和用戶)誰查看可以由授權和匿名用戶訪問的網頁。是否有辦法使兩種類型的操作都可以使用,因爲如果操作是[AllowAnonymous]
,則用戶未通過身份驗證,我無法檢索用戶標識,如果操作爲[Authorize]
,則匿名用戶無法訪問該頁面。ASP.NET Identity2 - 如何使用AllowAnonymous控制器獲取用戶ID?
[HttpGet]
[Route("{id}/Detail")]
[AllowAnonymous]
[ResponseType(typeof(JsonArticleDetail))]
public IHttpActionResult GetArticle(int id)
{
...
var entity = this.DbContext.Articles.Find(id);
var applicationUserId = User.Identity.IsAuthenticated ? User.Identity.GetUserId() : null;
entity.ArticleViews.Add(new ArticleView
{
ViewedOn = DateTime.UtcNow,
ApplicationUserId = applicationUserId
});
this.DbContext.SaveChanges();
return Ok(article);
}
那麼究竟是什麼問題都
[AllowAnonymous]
和[Authorize]
?你顯示的代碼應該工作。 – trailmax但是用戶在那一刻登錄? – trailmax