使用Microsoft Graph API进行服务帐户身份验证?

时间:2018-04-20 17:17:45

标签: microsoft-graph

我在MS Graph API之上构建服务器,我需要做的就是将图像上传并下载到OneDrive。应用程序的最终用户不会直接访问OneDrive,也不会拥有帐户。只需要应用程序本身以及拥有自己凭据的少数高级用户可以访问这些文件。

我希望能够将服务帐户的凭据配置为唯一访问存储桶的凭据,但似乎所有auth流都需要最终用户登录。是否有以API为中心的方式透明地完成这项工作?或者我应该从缺乏明确的支持推断出这不是OneDrive的有效用例,我应该看一下Azure Blob存储?

1 个答案:

答案 0 :(得分:0)

您可以使用类UserPasswordCredential(https://docs.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.clients.activedirectory.userpasswordcredential?view=azure-dotnet)使用用户名和密码对MS Graph API进行身份验证。样本:

UserCredential creds = new UserPasswordCredential("fred@contoso.onmicrosoft.com","password");
var bearerToken = AcquireToken("https://graph.microsoft.com", "d1ddf0e4-d672-4dae-b554-9d5bdfd93547", creds).Result;

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + result.AccessToken);
var graphInfo = client.GetAsync("https://graph.microsoft.com/v1.0/me/").Result;

警告:只有极少数情况下,直接使用用户名和密码是合法的(请参阅http://www.cloudidentity.com/blog/2014/07/08/using-adal-net-to-authenticate-users-via-usernamepassword/)。此外,DotNetCore中不提供UserPasswordCredential。

相关问题