[英]web api can't read claims on request
我正在將WebApi 2.0與Identity 2.0一起使用
我在startup.auth.cs中定義了ApplicationCookie身份驗證,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
CookieName = "MyAppCookieName"
});
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
我在AccountController中有操作要登錄:
var claims = new List<Claim>();
// create required claims
claims.Add(new Claim(ClaimTypes.NameIdentifier, "harryb"));
claims.Add(new Claim(ClaimTypes.Name, "harryb"));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties()
{
AllowRefresh = true,
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.AddDays(7),
}, identity);
return Ok();
到目前為止,在登錄請求中客戶端已收到cookie。 但是,當我提出新要求時,索賠是否為空? 我不想使用標准登錄和表格等。我只想對虛假登錄提出要求,並在下一個請求中閱讀..我所缺少的是什么?
Finnaly發現了問題所在..從這里: http : //www.asp.net/web-api/overview/security/individual-accounts-in-web-api
找到這個:
特別是,應用程序的MVC部分可能使用表單身份驗證,該身份驗證將憑據存儲在cookie中。 基於Cookie的身份驗證要求使用防偽令牌,以防止CSRF攻擊。 這是Web API的問題,因為Web API沒有方便的方法將防偽令牌發送到客戶端。 (有關此問題的更多背景,請參閱防止Web API中的CSRF攻擊。)調用SuppressDefaultHostAuthentication可確保Web API不受來自存儲在cookie中的憑據的CSRF攻擊的攻擊。
因此,在WebApiConfig中找到此// //將Web API配置為僅使用承載令牌身份驗證。 //config.SuppressDefaultHostAuthentication();
這給我帶來了一個問題..換句話說,您不能安全地在WebApi中使用Cookie身份驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.