參考文檔:ClaimsPrincipal Classgit
我的demo:SwaggerDemoApiapi
今天看到一段代碼懵逼了spa
var principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, authorization), new Claim(ClaimTypes.Name, userName.FirstOrDefault()) }, "Basic")); // authorize attribute request.GetRequestContext().Principal = principal;
是我項目中的代碼,忘記從哪拷貝來的了,不知道這段代碼的做用,看到官方文檔立馬明白了。code
我的理解:把header中的數據解析出來後,傳遞到接口裏面,若是有接口須要的話能夠直接解析獲得,代碼以下blog
ClaimsPrincipal principal = HttpContext.Current.User as ClaimsPrincipal; if (null != principal) { foreach (Claim claim in principal.Claims) { Response.Write("CLAIM TYPE: " + claim.Type + "; CLAIM VALUE: " + claim.Value + "</br>"); } }