Muh*_*eed 7 azure azure-active-directory
我一直在使用GitHub上Azure Active Directory的证书示例查看守护程序应用程序中的Azure AD官方身份验证.Web API服务似乎不了解客户端.
public IEnumerable Get()
{
//
// The Scope claim tells you what permissions the client application has in the service.
// In this case we look for a scope value of user_impersonation, or full access to the service as the user.
//
Claim scopeClaim = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/scope");
if (scopeClaim != null)
{
if (scopeClaim.Value != "user_impersonation")
{
throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized, ReasonPhrase = "The Scope claim does not contain 'user_impersonation' or scope claim not found" });
}
}
// A user's To Do list is keyed off of the NameIdentifier claim, which contains an immutable, unique identifier for the user.
Claim subject = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier);
return from todo in todoBag
where todo.Owner == subject.Value
select todo;
}
我是否认为在我的Azure AD中注册的任何客户端都可以通过设置此示例的方式访问Web API.
好问题,这无疑是误导性的。答案是肯定的 - 在 Azure AD 租户中注册的任何 Web 客户端都可以使用代码示例中描述的客户端凭据流程获取令牌来访问 Web API。
如果您不希望出现这种行为,您有 2 个选择:
示例代码对范围声明的使用确实具有误导性。该 API 旨在与代表用户(委托令牌)和使用应用程序身份(客户端凭据)访问 API 的客户端配合使用。这就是为什么你会在那里看到范围声明。
在运行时,您引用的验证逻辑会发现scopeClaim == null. 然后,它将使用ClaimTypes.NameIdentifier声明(也称为sub声明)来识别客户端应用程序以及属于该特定应用程序的 POST 或 GET 待办事项。
此示例不限制 Azure AD 租户中的哪些客户端可以访问 Web API。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
1073 次 |
| 最近记录: |