- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 Blazor 服务器端与 Microsoft Graph API 集成。我已经完成了以下工作
/*
* 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
关于c# - Blazor HttpContext Graph API 面临的挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60013263/
我对这两个概念感到困惑:In-graph replication和 Between-graph replication阅读 Replicated training 时在 tensorflow 的官方
我对这两个概念感到困惑:In-graph replication和 Between-graph replication阅读 Replicated training 时在 tensorflow 的官方
我正在尝试使用 https://graph.windows.net/{teantId}/users/[email protected]/thumbnailPhoto?api-version=1.6 访
我正在尝试使用 Graphs.jl 模块从 Julia 中的图中获取子图。我有图,并将其顶点和边存储到列表中,然后我的算法在该列表中移动并删除不属于新子图的节点和边。到这一部分,一切正常,在整个算法之
我是 Arangodb 的新手。我对使用哪个图形 API 感到困惑。我可以在 http://localhost:8529/ url 看到一张图。官方视频讨论了 Gremlin(我也安装了它)。然后就是
截至今天,文档建议使用 Microsoft Graph 而不是 Azure AD Graph API 来访问 Azure AD/B2C 资源。 之前,通过 Azure AD Graph API,我们可
我们希望将 .NET 应用从使用 Azure AD Graph 迁移到 Microsoft Graph API。目前我们正在使用包 Microsoft.WindowsAzure.Configurati
也许我遗漏了什么,但我不知道为什么 GraphQL 的标题中有 graph。 我猜这与 Graph Theory 有关和 graph并且可以看到某种联系,但如果有人能用简单的术语解释它就太好了。 最佳
我正在尝试使用API使用户的Facebook Pages具有已关联的Instagram企业帐户: https://graph.facebook.com/v2.7/me/accounts?field
如何导出我通过调用 GraphPlot 获得的输出的调整大小版本 (或 TreePlot 如果它们产生不同的输出)到 jpg 文件? 目前,我只是调用 Export[file_name, G]在哪里
如何在使用 gremlin 查询创建边缘之前检查边缘是否已存在?如何更新现有边缘而不是删除并重新创建? 最佳答案 我不确定您是否还在寻找答案;然而,简单的答案是 Cosmos DB 在 Gremlin
我使用的是 Xcode 10.2.1 和 macOS Catalina Developer Beta 2。每当我尝试使用内存图调试器时,我都会收到此错误: Memory Graph Debugger:
我正在设置一个机器人以在Facebook页面上自动发布。但是,当我运行脚本时,图形API会引发以下错误: Graph returned an error: (#200) Requires either
如何制定包含非英语字符(例如日耳曼语Umlauts)的Microsoft Graph /myOrganization/users OData查询? 例子: 我的租户中有一个名为“ThomasMülle
我正在寻找发布目标帖子时可以与Facebook Graph API一起使用的国家/州/城市列表。 我在this页面上找到了一个JSON文件,但是该文件无法正确解析,我也怀疑它是否可以用于发布目标,因为
关于 Graph API,帖子的分享数、帖子见解的分享数和页面上显示的分享数不相同。我假设这些代表相同的计数。我的假设错了吗? 来自帖子: https://graph.facebook.com/XXX
我正在尝试访问作为嵌套子站点一部分的列表的项目,如下所示: https://{mytenant}.sharepoint.com/ vendorSiteCollection/ v
我打算开发一个应用程序,但开发人员告诉我每个 IP 每 600 秒有 600 次调用的限制。该应用程序有很多场景,这还不够。有没有办法以某种方式增加限制?或者 Facebook 是否提供任何高级帐户或
我在 Neo4j 中创建了一张伦敦地铁 map 。站点通过 :CONNECTED_TO 关系连接,时间值表示停止之间需要多长时间(目前这些是我为测试输入的随机值)。位于多条线路上的车站每条线路都有一个
我正在尝试拉回所有用户的列表,我的预期结果将是大约 20,000 个用户。 图表似乎将我限制为 1000。 图调用https://graph.microsoft.com/v1.0/users返回 10
我是一名优秀的程序员,十分优秀!