我正在开发.NET Core应用程序,Web Api并集成Azure B2C身份验证。现在我需要阅读声明,并且由于某种原因我得到null甚至我可以看到堆栈中的值期望身份对象值正在工作,不确定我在拼图中缺少什么
var userId = identity?.Claims.SingleOrDefault(claim => claim.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
var UserId = listedUserGuid;
var Email = identity?.Claims.SingleOrDefault(claim => claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value;
var Name = identity?.Claims.SingleOrDefault(claim => claim.Type == ClaimTypes.Name).Value;
var Surname = identity?.Claims.SingleOrDefault(claim => claim.Type == ClaimTypes.Surname).Value;
答案 0 :(得分:1)
在ASP.NET Core 2.0中,您还可以使用便捷方法FindFirst
:
User.FindFirst("email");
所以你甚至不必担心身份属性。
答案 1 :(得分:0)
找到答案;
var UserId = listedUserGuid;
var Email = identity?.Claims.SingleOrDefault(claim => claim.Type == "emails")?.Value;
var Name = identity?.Claims.SingleOrDefault(claim => claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname")?.Value;
var Surname = identity?.Claims.SingleOrDefault(claim => claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname")?.Value;