日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

跨平台应用集成(在ASP.NET Core MVC 应用程序中集成 Microsoft Graph)

發布時間:2023/12/4 asp.net 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 跨平台应用集成(在ASP.NET Core MVC 应用程序中集成 Microsoft Graph) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1談一談.NET 的跨平臺


終于要寫到這一篇了。跨平臺的支持可以說是 Office 365 平臺在設計伊始就考慮的目標。我在前面的文章已經提到過了,Microsoft Graph 服務針對一些主流的開源平臺(主要用來做跨平臺應用)都有支持,例如 python,nodejs 等。他們真的非常好用,與此同時我雖然對他們也有一定的了解,但要跟我最熟悉的 Microsoft .NET 來比較的話,我自然還是更喜歡后者了。


所以,一直在等待合適的時間,要來寫 Microsoft .NET 的跨平臺應用,這是多么令人期待的事情啊。經過一段時間的研究,我今天正式隆重地給大家介紹,如何在 ASP.NET Core 平臺上面構建一個 MVC 應用程序,并且在里面集成 Microsoft Graph。


關于Microsoft .NET 這幾年的發展,我是感到比較興奮的,作為一個從.NET 1.1就開始追隨的骨灰級粉絲,我很高興地看到現在.NET 已經真正邁出了跨平臺的腳步,而且完全開源了。如果要講這個話題,恐怕我是一時半會剎不住車的,所以我就此打住吧,有興趣的朋友們可以通過下面這個網址了解更多.NET 的發展情況。?


這一篇文章用到的技術,是最新的.NET Core 中 ASP.NET Core 提供的,我使用了其中的 MVC 這個模板創建了一個簡單的應用程序,并且略微改造了一下,使其能夠采用 Azure AD 進行身份驗證,繼而通過獲得的用戶憑據能實現對 Microsoft Graph 的使用。





2ASP.NET Core MVC 整合了 Graph 的場景效果




我已經編寫好了一個完整的范例,請大家通過下面的地址進行下載,我后面將大致提到一些重點的功能是如何實現的?https://github.com/chenxizhang/office365dev/tree/master/samples/aspnetcoremvc

在準備這個范例,以及編寫這個文章的時候,為了全面地測試在跨平臺開發方面的能力,我完全采用了一臺全新的 MacBook 作為工作用機,開發工具我使用的是 Visual Studio Code(這個直接就可以在 Mac 里面運行),就像你看到的這樣。



好的,大致背景我也交代清楚了,如果大家下載了代碼,可以跟我一起來體驗一下這個應用程序運行起來的效果吧 —— 我推薦你也用 Visual Studio Code 來打開這個應用程序。(是的,你不再需要安裝 Visual Studio 完整版)


打開命令行工具,我們很快要運行幾個命令?


請運行下面的命令,下載當前項目所依賴的一些組件包


dotnet restore


然后運行下面的命令,可以將當前項目運行起來


dotnet run


如果不出意外的話,我寫好的這個簡單的應用程序會啟動起來,并且在本機的5000端口進行監聽



看起來跟我們一般的 MVC 程序真的是一樣一樣的,此時,請點擊頁面頂部左上角的“About”,看看會發生什么呢



輸入正確的用戶名和密碼后,你就可以看到該用戶的基本信息了



好了,功能確實就是這樣,足夠簡單,不是嗎?但是正如我猜想你應該會想到的那樣,只要打開了 Graph 這扇大門,無窮的寶藏就等著你盡情地創造性地利用了。




3代碼解析




下面我還是簡單地講解一下我在標準的模板基礎上做過哪些定制,從而實現了上面的功能的。


要創建這個應用程序,你需要安裝 dotnet sdk(https://www.microsoft.com/net/download/core?),然后在本地命令行工具中運行 dotnet new mvc 即可


首先,我為項目添加了幾個外部組件包,這是通過修改項目定義文件(aspnetcoremvc.csproj)來實現的。


<PackageReference Include="Microsoft.Graph" Version="1.4.0"/> <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="1.1.0"/> <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0"/> <PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.13.9"/> <PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.0"/> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2"/>


這些組件都是托管在 nuget.org 這個網站上面,甚至整個 dotnet core 的核心組件也都是開源托管在這個上面。一般添加完這些組件后,都需要運行 dotnet restore 命令在本地進行還原。


然后,我修改了 Startup.cs 文件。這是 asp.net core 應用程序的一個標準文件,用來定義程序入口,加載相關服務和中間件。(這里涉及的知識面太多,以至于我無法一一說明,有興趣可以參考?https://asp.net/core了解。


using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; //這里增加了一寫命名空間導入 using Microsoft.Extensions.Caching; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Session; using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.AspNetCore.Authentication; using System.Security.Claims; using Microsoft.AspNetCore.Http;namespace aspntecoremvc {public class Startup{// This method gets called by the runtime. Use this method to add services to the container.//這里引入一些服務,注入一些組件public void ConfigureServices(IServiceCollection services){services.AddSession();services.AddAuthentication(sharedoptions => sharedoptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);// Add framework services.services.AddMvc();}//這里定義了一些靜態信息private readonly string ClientId="e91ef175-e38d-4feb-b1ed-f243a6a81b93";private readonly string Authority=String.Format("https://login.microsoftonline.com/{0}","office365devlabs.onmicrosoft.com");private readonly string ClientSecret="2F5jdoGGNn59oxeDLE9fXx5tD86uvzIji74dmLaj3YI=";private readonly string GraphResourceId="https://graph.microsoft.com";private readonly string CallbackPath ="/signin-oidc";// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole();loggerFactory.AddDebug();if (env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseBrowserLink();}else{app.UseExceptionHandler("/Home/Error");}app.UseStaticFiles();//這里幾步是最關鍵的,定義了如何進行身份認證以及如何保存app.UseSession();app.UseCookieAuthentication();app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions{ClientId = ClientId,Authority = Authority,ClientSecret = ClientSecret,ResponseType = OpenIdConnectResponseType.CodeIdToken,CallbackPath = CallbackPath,GetClaimsFromUserInfoEndpoint =true,Events = new OpenIdConnectEvents{OnAuthorizationCodeReceived = OnAuthorizationCodeReceived}});app.UseMvc(routes =>{routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");});}private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context){// 將 Token 信息保存在 session 里面,后續 Graph 就可以直接調用了string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);AuthenticationContext authContext = new AuthenticationContext(Authority, new SampleSessionCache(userObjectId, context.HttpContext.Session));AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, GraphResourceId);}private Task OnAuthenticationFailed(FailureContext context){context.HandleResponse();context.Response.Redirect("/Home/Error?message=" + context.Failure.Message);return Task.FromResult(0);}} }


我還專門定義了一個簡單的類用來保存 Token 信息。為了簡單起見,我們將 Token 保存在 Session 里面,這樣的話,用戶登陸一次后,在一個會話里面就不需要多次登錄,而是可以直接重用這些 Token。


這個類其實我是重用了之前在 ASP.NET MVC 開發中的那個類,沒有什么特別要交待的,請直接打開 SampleSessionCache.cs 這個文件了解即可。


接下來,為了便于后續在 Controller 里面快速地訪問到 Graph,我對 GraphServiceClient 進行了封裝,請參考 SDKHelper.cs 這個文件


using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.Graph; using System.Net.Http.Headers; using Microsoft.AspNetCore.Http;namespace aspntecoremvc{public static class SDKHelper{//這里其實是對 ControllerBase 這個類型進行了擴展public static async Task<GraphServiceClient> GetAuthenticatedClient(this ControllerBase controller){var Authority = String.Format("https://login.microsoftonline.com/{0}","office365devlabs.onmicrosoft.com");var ClientId = "e91ef175-e38d-4feb-b1ed-f243a6a81b93";var ClientSecret = "2F5jdoGGNn59oxeDLE9fXx5tD86uvzIji74dmLaj3YI=";var GraphResourceId = "https://graph.microsoft.com";string userObjectId = controller.HttpContext.User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);AuthenticationContext authContext = new AuthenticationContext(Authority, new SampleSessionCache(userObjectId, controller.HttpContext.Session));AuthenticationResult result = await authContext.AcquireTokenSilentAsync(GraphResourceId,ClientId);GraphServiceClient client = new GraphServiceClient(new DelegateAuthenticationProvider(async request=>{request.Headers.Authorization = new AuthenticationHeaderValue("bearer", result.AccessToken);await Task.FromResult(0);}));return client;}}}


有了上面的準備,在真正需要用到 Graph 服務的地方,我們的代碼是非常簡單的,請參考 HomeController.cs 文件中的 About 方法


[Authorize]//用這個標記該方法需要用戶登錄 public async Task<IActionResult> About() {var client = await this.GetAuthenticatedClient(); ?//獲取用戶詳細信息,然后傳遞給視圖return View(await client.Me.Request().GetAsync()); }

到這里為止,我的這個例子的主要代碼就解釋完了。你可能會覺得,這太簡單了吧。如果你這樣認為,我一方面感到很高興,因為這是我希望呈現出來的效果;另一方面我要提醒你的是,由于 asp.net core 是一個還比較新的技術,這方面的材料相當少,其實我還是做了相當多的研究才精煉成這樣的,其間遇到過多少坑,多少曲折迂回,不足以外人道也,但我很看好 asp.net core,并且將持續在此之上進行投資,這也幾乎是可以肯定的。




4結語




這個例子實現的功能并沒有什么驚天動地的,但與咱們之前一系列的范例相呼應的是,我是要幫助大家打開 Microsoft Graph 的大門,至于你要怎么去利用里面豐富的寶藏,我就選擇的權利交給你自己。


寫到這里,我的這個系列文章的第一個大的里程碑應該是要實現了。我用了將近四個月的時間,寫了十幾篇跟 Office 365 開發入門,以及具體的 Microsoft Graph 開發有關的文章,差不多算是比較完整了。


我還將繼續寫后續的內容,例如 Office Add-ins,SharePoint 開發,Teams 開發(Bot 等),有興趣的朋友們可繼續關注。




?更新一




作為一個不斷追求代碼復用的程序猿,我這兩天在上面這個范例基礎上對代碼進行了一定的封裝,如果你此時查看代碼的話,會發現已經有了較大的不同。


首先,我將所有公用的代碼全部提取到了一個單獨的項目(Office365GraphCoreMVCHelper?)中,這里面的關鍵代碼有

一個用來讀取配置文件的類型


namespace Office365GraphCoreMVCHelper {public class AppSetting{public Info Office365ApplicationInfo { get; set; }public class Info{public string ClientId { get; set; }public string ClientSecret { get; set; }public string Authority { get; set; }public string GraphResourceId { get; set; }}} }


一個可公用的Startup類型


using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Caching; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Session; using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.AspNetCore.Authentication; using System.Security.Claims; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options;namespace Office365GraphCoreMVCHelper {public class Startup{static IConfigurationRoot Configuration { get; set; }public Startup(IHostingEnvironment env){Configuration = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json").Build();}// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){//這里將配置信息注入到應用程序中services.AddOptions();services.Configure<AppSetting>(Configuration);services.AddSession();services.AddAuthentication(sharedoptions => sharedoptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);// Add framework services.services.AddMvc();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole();loggerFactory.AddDebug();if (env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseBrowserLink();}else{app.UseExceptionHandler("/Home/Error");}//ConfigureMiddleware(app,env,loggerFactory);app.UseStaticFiles();app.UseSession();app.UseCookieAuthentication();//獲得之前注入的配置信息var options = app.ApplicationServices.GetRequiredService<IOptions<AppSetting>>();app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions{ClientId = options.Value.Office365ApplicationInfo.ClientId,Authority = options.Value.Office365ApplicationInfo.Authority,ClientSecret = options.Value.Office365ApplicationInfo.ClientSecret,ResponseType = OpenIdConnectResponseType.CodeIdToken,CallbackPath = "/signin-oidc",GetClaimsFromUserInfoEndpoint = true,Events = new OpenIdConnectEvents{OnAuthorizationCodeReceived = async (context) =>{string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;ClientCredential clientCred = new ClientCredential(options.Value.Office365ApplicationInfo.ClientId, options.Value.Office365ApplicationInfo.ClientSecret);AuthenticationContext authContext = new AuthenticationContext(options.Value.Office365ApplicationInfo.Authority, new SampleSessionCache(userObjectId, context.HttpContext.Session));AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, options.Value.Office365ApplicationInfo.GraphResourceId);}}});app.UseMvc(routes =>{routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");});}private Task OnAuthenticationFailed(FailureContext context){context.HandleResponse();context.Response.Redirect("/Home/Error?message=" + context.Failure.Message);return Task.FromResult(0);}} }

改造過的SDKHelper類型,主要增加了從配置文件中讀取信息的功能


using System; using System.Net.Http.Headers; using System.Threading.Tasks; using Microsoft.Graph; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Builder;namespace Office365GraphCoreMVCHelper {public static class SDKHelper{public static async Task<GraphServiceClient> GetAuthenticatedClient(this ControllerBase controller,IOptions<AppSetting> options){var Authority = options.Value.Office365ApplicationInfo.Authority;var ClientId = options.Value.Office365ApplicationInfo.ClientId;var ClientSecret = options.Value.Office365ApplicationInfo.ClientSecret;var GraphResourceId = options.Value.Office365ApplicationInfo.GraphResourceId;string userObjectId = controller.HttpContext.User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);AuthenticationContext authContext = new AuthenticationContext(Authority, new SampleSessionCache(userObjectId, controller.HttpContext.Session));AuthenticationResult result = await authContext.AcquireTokenSilentAsync(GraphResourceId, ClientId);GraphServiceClient client = new GraphServiceClient(new DelegateAuthenticationProvider(async request =>{request.Headers.Authorization = new AuthenticationHeaderValue("bearer", result.AccessToken);await Task.FromResult(0);}));return client;}} }


由于有了這個公用的組件,那么在aspnetcoremvc這個主程序中,我可以極大地簡化代碼。


首先,我在項目文件中定義了對公用組件的引用


<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>netcoreapp1.1</TargetFramework></PropertyGroup><ItemGroup><PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /><PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" /><PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" /><PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" /><PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" /><PackageReference Include="Microsoft.Graph" Version="1.4.0"/><PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="1.1.0"/><PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0"/><PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.13.9"/><PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.0"/><PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2"/></ItemGroup><ItemGroup> ? <ProjectReference Include="..\Office365GraphCoreMVCHelper\Office365GraphCoreMVCHelper.csproj" /></ItemGroup> ?</Project>


然后,我刪除了該項目中的Startup類型,取而代之的在Program中直接引用公用組件中定義好的那個Startup


using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Office365GraphCoreMVCHelper;namespace aspntecoremvc {public class Program{public static void Main(string[] args){var host = new WebHostBuilder().UseSetting("startupAssembly","Office365GraphCoreMVCHelper").UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseIISIntegration().Build();host.Run();}} }


當然,我們需要定義一個配置文件來保存clientId等信息,該文件命名為appsettings.json


{"Office365ApplicationInfo":{"ClientId":"e91ef175-e38d-4feb-b1ed-f243a6a81b93","ClientSecret":"2F5jdoGGNn59oxeDLE9fXx5tD86uvzIji74dmLaj3YI=","Authority":"https://login.microsoftonline.com/office365devlabs.onmicrosoft.com","GraphResourceId":"https://graph.microsoft.com"} }


最后,在HomeController(或者同類需要用到Microsoft Graph的Controller)中,通過下面的代碼來實現調用


using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.Graph; using System.Net.Http.Headers; using Office365GraphCoreMVCHelper; using Microsoft.Extensions.Options;namespace aspntecoremvc.Controllers {public class HomeController : Controller{private readonly IOptions<AppSetting> Options;public HomeController(IOptions<AppSetting> options){this.Options = options;}public IActionResult Index(){return View();}[Authorize]public async Task<IActionResult> About(){var client = await this.GetAuthenticatedClient(this.Options); ? ? ? ? ? ?return View(await client.Me.Request().GetAsync());}public IActionResult Contact(){ViewData["Message"] = "Your contact page.";return View();}public IActionResult Error(){return View();}} }




? ? ? ??更新二




我進一步上面分離出來的這個Office365GraphCoreMVCHelper的項目打包成了一個nuget的packagehttps://www.nuget.org/packages/Office365GraphCoreMVCHelper/,以便實現更大范圍的復用。



如何使用它呢?很簡單,請按照下面的步驟即可


  • 創建一個ASP.NET Core MVC項目

    dotnet new mvc


  • 增加對于Office365GraphCoreMVCHelper的引用,修改csproj文件,添加如下的定義

  • 下載這個包

    dotnet restore


  • 修改Program.cs文件,使用Office365GraphCoreMVCHelper 定義好的Startup類(增加下面代碼中的UseSetting這一句)。當前項目的Startup.cs文件可以刪除。

  • using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting;namespace testaspnetcoremvc {public class Program{public static void Main(string[] args){var host = new WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseIISIntegration().UseSetting("startupAssembly","Office365GraphCoreMVCHelper").Build();host.Run();}} }


    修改appsettings.json文件,確保里面有如下Office365ApplicationInfo信息


    {"Office365ApplicationInfo":{"ClientId":"e91ef175-e38d-4feb-b1ed-f243a6a81b93","ClientSecret":"2F5jdoGGNn59oxeDLE9fXx5tD86uvzIji74dmLaj3YI=","Authority":"https://login.microsoftonline.com/office365devlabs.onmicrosoft.com","GraphResourceId":"https://graph.microsoft.com"},"Logging": {"IncludeScopes": false,"LogLevel": {"Default": "Warning"}} }


    修改HomeController,在需要進行身份驗證以及調用Graph API的地方使用如下的代碼即可

    //添加幾個引用 using Office365GraphCoreMVCHelper; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Authorization;//修改構造函數,接受注入的配置信息 private IOptions<AppSetting> settings; public HomeController(IOptions<AppSetting> options) {settings = options; }//在需要調用Graph API的Action中做如下修改 [Authorize] public async Task<IActionResult> About() {var client = await this.GetAuthenticatedClient(settings);var user = client.Me.Request().GetAsync().Result;ViewData["Message"] = $"Hello,{user.DisplayName}";return View(); }


    原文地址:https://chenxizhang.gitbooks.io/office365devguide/content/docs/crossplatform.html


    .NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

    總結

    以上是生活随笔為你收集整理的跨平台应用集成(在ASP.NET Core MVC 应用程序中集成 Microsoft Graph)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。