gpt4 book ai didi

c# - Blazor HttpContext Graph API 面临的挑战

转载 作者:行者123 更新时间:2023-12-03 00:04:12 29 4
gpt4 key购买 nike

我正在尝试将 Blazor 服务器端与 Microsoft Graph API 集成。我已经完成了以下工作

  1. 通过 Visual Studio 新建项目向导注册 Azure AD 签名应用程序
  2. 已验证应用程序确实使用 Azure AD 对用户进行身份验证
  3. 然后我尝试从这里迁移图形示例代码:https://learn.microsoft.com/en-us/samples/microsoftgraph/aspnetcore-connect-sample/microsoft-graph-connect-sample-for-aspnet-core-21/即GraphAuthProvider.cs、GraphSdkHelper.cs、GraphService.cs(代码如下)
/* 
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the source repository root for complete license information.
*/

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Graph;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MicrosoftGraphAspNetCoreConnectSample.Helpers
{
public static class GraphService
{
// Load user's profile in formatted JSON.
public static async Task<string> GetUserJson(GraphServiceClient graphClient, string email, HttpContext httpContext)
{
if (email == null) return JsonConvert.SerializeObject(new { Message = "Email address cannot be null." }, Formatting.Indented);

try
{
// Load user profile.
var user = await graphClient.Users[email].Request().GetAsync();
return JsonConvert.SerializeObject(user, Formatting.Indented);
}
catch (ServiceException e)
{
switch (e.Error.Code)
{
case "Request_ResourceNotFound":
case "ResourceNotFound":
case "ErrorItemNotFound":
case "itemNotFound":
return JsonConvert.SerializeObject(new { Message = $"User '{email}' was not found." }, Formatting.Indented);
case "ErrorInvalidUser":
return JsonConvert.SerializeObject(new { Message = $"The requested user '{email}' is invalid." }, Formatting.Indented);
case "AuthenticationFailure":
return JsonConvert.SerializeObject(new { e.Error.Message }, Formatting.Indented);
case "TokenNotFound":
await httpContext.ChallengeAsync();
return JsonConvert.SerializeObject(new { e.Error.Message }, Formatting.Indented);
default:
return JsonConvert.SerializeObject(new { Message = "An unknown error has occurred." }, Formatting.Indented);
}
}
}

// Load user's profile picture in base64 string.
public static async Task<string> GetPictureBase64(GraphServiceClient graphClient, string email, HttpContext httpContext)
{
try
{
// Load user's profile picture.
var pictureStream = await GetPictureStream(graphClient, email, httpContext);

// Copy stream to MemoryStream object so that it can be converted to byte array.
var pictureMemoryStream = new MemoryStream();
await pictureStream.CopyToAsync(pictureMemoryStream);

// Convert stream to byte array.
var pictureByteArray = pictureMemoryStream.ToArray();

// Convert byte array to base64 string.
var pictureBase64 = Convert.ToBase64String(pictureByteArray);

return "data:image/jpeg;base64," + pictureBase64;
}
catch (Exception e)
{
switch (e.Message)
{
case "ResourceNotFound":
// If picture not found, return the default image.
return "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjwhRE9DVFlQRSBzdmcgIFBVQkxJQyAnLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4nICAnaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkJz4NCjxzdmcgd2lkdGg9IjQwMXB4IiBoZWlnaHQ9IjQwMXB4IiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDMxMi44MDkgMCA0MDEgNDAxIiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjMxMi44MDkgMCA0MDEgNDAxIiB4bWw6c3BhY2U9InByZXNlcnZlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KPGcgdHJhbnNmb3JtPSJtYXRyaXgoMS4yMjMgMCAwIDEuMjIzIC00NjcuNSAtODQzLjQ0KSI+DQoJPHJlY3QgeD0iNjAxLjQ1IiB5PSI2NTMuMDciIHdpZHRoPSI0MDEiIGhlaWdodD0iNDAxIiBmaWxsPSIjRTRFNkU3Ii8+DQoJPHBhdGggZD0ibTgwMi4zOCA5MDguMDhjLTg0LjUxNSAwLTE1My41MiA0OC4xODUtMTU3LjM4IDEwOC42MmgzMTQuNzljLTMuODctNjAuNDQtNzIuOS0xMDguNjItMTU3LjQxLTEwOC42MnoiIGZpbGw9IiNBRUI0QjciLz4NCgk8cGF0aCBkPSJtODgxLjM3IDgxOC44NmMwIDQ2Ljc0Ni0zNS4xMDYgODQuNjQxLTc4LjQxIDg0LjY0MXMtNzguNDEtMzcuODk1LTc4LjQxLTg0LjY0MSAzNS4xMDYtODQuNjQxIDc4LjQxLTg0LjY0MWM0My4zMSAwIDc4LjQxIDM3LjkgNzguNDEgODQuNjR6IiBmaWxsPSIjQUVCNEI3Ii8+DQo8L2c+DQo8L3N2Zz4NCg==";
case "EmailIsNull":
return JsonConvert.SerializeObject(new { Message = "Email address cannot be null." }, Formatting.Indented);
default:
return null;
}
}
}

public static async Task<Stream> GetPictureStream(GraphServiceClient graphClient, string email, HttpContext httpContext)
{
if (email == null) throw new Exception("EmailIsNull");

Stream pictureStream = null;

try
{
try
{
// Load user's profile picture.
pictureStream = await graphClient.Users[email].Photo.Content.Request().GetAsync();
}
catch (ServiceException e)
{
if (e.Error.Code == "GetUserPhoto") // User is using MSA, we need to use beta endpoint
{
// Set Microsoft Graph endpoint to beta, to be able to get profile picture for MSAs
graphClient.BaseUrl = "https://graph.microsoft.com/beta";

// Get profile picture from Microsoft Graph
pictureStream = await graphClient.Users[email].Photo.Content.Request().GetAsync();

// Reset Microsoft Graph endpoint to v1.0
graphClient.BaseUrl = "https://graph.microsoft.com/v1.0";
}
}
}
catch (ServiceException e)
{
switch (e.Error.Code)
{
case "Request_ResourceNotFound":
case "ResourceNotFound":
case "ErrorItemNotFound":
case "itemNotFound":
case "ErrorInvalidUser":
// If picture not found, return the default image.
throw new Exception("ResourceNotFound");
case "TokenNotFound":
await httpContext.ChallengeAsync();
return null;
default:
return null;
}
}

return pictureStream;
}
public static async Task<Stream> GetMyPictureStream(GraphServiceClient graphClient, HttpContext httpContext)
{
Stream pictureStream = null;

try
{
try
{
// Load user's profile picture.
pictureStream = await graphClient.Me.Photo.Content.Request().GetAsync();
}
catch (ServiceException e)
{
if (e.Error.Code == "GetUserPhoto") // User is using MSA, we need to use beta endpoint
{
// Set Microsoft Graph endpoint to beta, to be able to get profile picture for MSAs
graphClient.BaseUrl = "https://graph.microsoft.com/beta";

// Get profile picture from Microsoft Graph
pictureStream = await graphClient.Me.Photo.Content.Request().GetAsync();

// Reset Microsoft Graph endpoint to v1.0
graphClient.BaseUrl = "https://graph.microsoft.com/v1.0";
}
}
}
catch (ServiceException e)
{
switch (e.Error.Code)
{
case "Request_ResourceNotFound":
case "ResourceNotFound":
case "ErrorItemNotFound":
case "itemNotFound":
case "ErrorInvalidUser":
// If picture not found, return the default image.
throw new Exception("ResourceNotFound");
case "TokenNotFound":
await httpContext.ChallengeAsync();
return null;
default:
return null;
}
}

return pictureStream;
}

// Send an email message from the current user.
public static async Task SendEmail(GraphServiceClient graphClient, IHostingEnvironment hostingEnvironment, string recipients, HttpContext httpContext)
{
if (recipients == null) return;

var attachments = new MessageAttachmentsCollectionPage();

try
{
// Load user's profile picture.
var pictureStream = await GetMyPictureStream(graphClient, httpContext);

if (pictureStream != null)
{
// Copy stream to MemoryStream object so that it can be converted to byte array.
var pictureMemoryStream = new MemoryStream();
await pictureStream.CopyToAsync(pictureMemoryStream);

// Convert stream to byte array and add as attachment.
attachments.Add(new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = pictureMemoryStream.ToArray(),
ContentType = "image/png",
Name = "me.png"
});
}
}
catch (Exception e)
{
switch (e.Message)
{
case "ResourceNotFound":
break;
default:
throw;
}
}

// Prepare the recipient list.
var splitRecipientsString = recipients.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
var recipientList = splitRecipientsString.Select(recipient => new Recipient
{
EmailAddress = new EmailAddress
{
Address = recipient.Trim()
}
}).ToList();

// Build the email message.
var email = new Message
{
Body = new ItemBody
{
Content = System.IO.File.ReadAllText(hostingEnvironment.WebRootPath + "/email_template.html"),
ContentType = BodyType.Html,
},
Subject = "Sent from the Microsoft Graph Connect sample",
ToRecipients = recipientList,
Attachments = attachments
};

await graphClient.Me.SendMail(email, true).Request().PostAsync();
}
}
}

即使升级到所有最新的 DLL,上述代码也可以在示例应用程序中运行。

当页面在第一次调用时尝试调用 GetUserJson 时,它会指出“由于响应已启动,无法修改 header ”或非常类似的内容。

有人知道如何从 blazor Azure AD 登录中获取图形的身份验证 token 吗?

最佳答案

查看 GitHub 上的 BlazorGraphApi

具有 AD 身份验证的 Blazor 服务器端,使用 Microsoft.Idenity.Web 代表登录用户调用 MS Graph API

https://github.com/wmgdev/BlazorGraphApi

关于c# - Blazor HttpContext Graph API 面临的挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60013263/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com