{"id":225,"date":"2020-07-01T11:09:03","date_gmt":"2020-07-01T02:09:03","guid":{"rendered":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=225"},"modified":"2020-07-01T11:09:03","modified_gmt":"2020-07-01T02:09:03","slug":"microsoft-graph%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6%e3%80%81%e3%83%a6%e3%83%bc%e3%82%b6%e3%81%8c%e3%82%b0%e3%83%ab%e3%83%bc%e3%83%97%e3%81%ab%e5%b1%9e%e3%81%97%e3%81%a6%e3%81%84%e3%82%8b","status":"publish","type":"post","link":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=225","title":{"rendered":"Microsoft Graph\u3092\u4f7f\u7528\u3057\u3066\u3001\u30e6\u30fc\u30b6\u304c\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u8abf\u3079\u308b"},"content":{"rendered":"\n<p>\u3046\u3061\u306e\u793e\u5185\u30a2\u30d7\u30ea\u306e\u8a8d\u8a3c\u65b9\u6cd5\u306f\u4eca\u307e\u3067\u30d0\u30e9\u30d0\u30e9\u3067\u3001\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3068\u304b\u975e\u5e38\u306b\u5927\u5909\u3060\u3063\u305f\u306e\u3067\u3001\u4eca\u5f8c\u306fAzure AD\u306b\u7d71\u4e00\u3057\u3066\u3044\u304d\u305f\u3044\u306a\u3068\u601d\u3063\u3066\u3044\u308b\u4eca\u65e5\u3053\u306e\u9803\u3002<\/p>\n\n\n\n<p>\u3068\u308a\u3042\u3048\u305a\u3001Microsoft Graph API\u3092\u4f7f\u7528\u3057\u3066\u3001\u8a8d\u8a3c\u3068\u30a2\u30af\u30bb\u30b9\u6a29\u30c1\u30a7\u30c3\u30af(\u6307\u5b9a\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3067\u5224\u65ad\u3059\u308b)\u3092\u8a66\u3057\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<p>dotnet core 3.1\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u3067\u5b9f\u88c5\u3002<br>\u307e\u305a\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306bMicrosoft.Graph,Microsoft.Graph.Auth,Microsoft.Identity.Client\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n\n\n\n<p>Web\u30a2\u30d7\u30ea\u3067\u306f\u306a\u3044\u306e\u3067\u3001\u4eca\u56de\u306f\u524d\u56de\u3068\u540c\u69d8\u3001\u30e6\u30fc\u30b6\u540d\u30fb\u30d1\u30b9\u30ef\u30fc\u30c9\u6307\u5b9a\u3067Token\u3092\u53d6\u5f97\u3059\u308b\u3002(\u5b9f\u969b\u306e\u53d6\u5f97\u306fGraph API\u5185\u90e8\u3067\u884c\u3046)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u306e\u53d6\u5f97\" data-lang=\"C#\"><code>IPublicClientApplication app = PublicClientApplicationBuilder\n    .Create(ApplicationID)\n    .WithAuthority(Authority)\n    .Build();\n\/\/ Scope\u306f\u30e6\u30fc\u30b6\u60c5\u5831\u306e\u8aad\u53d6\u3068\u30b0\u30eb\u30fc\u30d7\u60c5\u5831\u306e\u8aad\u53d6\u304c\u3042\u308c\u3070\u3044\u3044\u304b\u306a\nstring[] scopes = new string[] { &quot;https:\/\/graph.microsoft.com\/User.Read&quot;, &quot;https:\/\/graph.microsoft.com\/Group.Read.All&quot; };\n\n\/\/ \u30e6\u30fc\u30b6\u540d\u30fb\u30d1\u30b9\u30ef\u30fc\u30c9\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u306e\u3067\u3001Microsoft.Graph.Auth.UserNamePasswordProvider\u3092\u4f7f\u7528\u3059\u308b\nvar provider = new UsernamePasswordProvider(app,scopes);\n\n\/\/ Microsoft.GraphGraphServiceClient\u3092\u4f5c\u6210\nvar graphClient = new GraphServiceClient(provider);\n\n\/\/ \u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u306e\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\nvar me = await graphClient.Me.Request()\n    .WithUsernamePassword(username,secPwd)\n    .GetAsync();\n\n\/\/ \u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u304c\u5c5e\u3057\u3066\u3044\u308b\u3001\u30b0\u30eb\u30fc\u30d7\u3092\u53d6\u5f97\u3059\u308b\nvar members = await graphClient.Me.MemberOf.Request().GetAsync();\nif (members != null) {\n    foreach(var m in members) {\n        if (m.GetType() == typeof(Group)) {\n            Group g = (Group)m;\n            Console.WriteLine($&quot;{g.Id} : {g.DisplayName}&quot;);\n        }\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>MemberOf\u306f\u81ea\u5206\u304c\u5c5e\u3057\u3066\u3044\u308b\u30b0\u30eb\u30fc\u30d7\u306e\u4e00\u89a7\u306a\u306e\u3067\u3001\u3053\u306e\u30b0\u30eb\u30fc\u30d7\u304c\u4ed6\u306e\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u306e\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u3053\u3068\u306f\u3053\u306e\u4e00\u89a7\u304b\u3089\u306f\u78ba\u8a8d\u3067\u304d\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u3064\u307e\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u72b6\u614b\u3067\u3001\u81ea\u5206\u304c\u6700\u7d42\u7684\u306bA\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u306f\u3053\u306e\u4e00\u89a7\u304b\u3089\u306f\u5206\u304b\u3089\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3002MemberOf\u3067\u53d6\u5f97\u3055\u308c\u308b\u306e\u306f\u3001B\u30b0\u30eb\u30fc\u30d7\u3068C\u30b0\u30eb\u30fc\u30d7\u3068\u306a\u308b\u304b\u3089\u3060\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sumomo.ohwaki.jp\/wordpress\/wp-content\/uploads\/2020\/07\/adgrp.png\" alt=\"\" class=\"wp-image-227\" width=\"466\" height=\"379\"\/><\/figure>\n\n\n\n<p>\u3053\u3046\u3044\u3046\u30b0\u30eb\u30fc\u30d7\u306e\u5165\u308c\u5b50\u306f\u914d\u5e03\u30ea\u30b9\u30c8\u306a\u3069\u306b\u306f\u826f\u304f\u898b\u3089\u308c\u308b\u3088\u306d\u3002\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30b0\u30eb\u30fc\u30d7\u306f\u3053\u3046\u3044\u3046\u8a2d\u8a08\u3092\u3057\u306a\u3044\u65b9\u304c\u826f\u3044\u3068\u306f\u601d\u3046\u3051\u3069\u3001\u5834\u5408\u306b\u3088\u308a\u3051\u308a\u3060\u308d\u3046\u304b\u30fb\u30fb\u30fb<\/p>\n\n\n\n<p>\u3067\u3001\u81ea\u5206\u304cA\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u8abf\u3079\u308b\u306b\u306f\u3001\u30b0\u30eb\u30fc\u30d7\u3092\u624b\u7e70\u3063\u3066\u3044\u304f\u5fc5\u8981\u304c\u3042\u308b\u3002\u52b9\u7387\u306f\u3068\u3082\u304b\u304f\u3068\u3057\u3066\u3001\u3068\u308a\u3042\u3048\u305a\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"\u6307\u5b9a\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u8abf\u3079\u308b\" data-lang=\"C#\"><code>\/\/ \u30b0\u30eb\u30fc\u30d7\u3092\u624b\u7e70\u3063\u3066\u3001\u6307\u5b9a\u30b0\u30eb\u30fc\u30d7\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u8abf\u3079\u308b\n\/\/ \u6700\u521d\u306b\u6307\u5b9a\u3059\u308b\u30b0\u30eb\u30fc\u30d7\u304c\u6307\u5b9a\u30b0\u30eb\u30fc\u30d7\nstatic async Task&lt;bool&gt; IsMember(Group g,string mail,GraphServiceClient graphClient,string username, SecureString secPwd) {\n    foreach(var m in g.Members) {\n        if (m.GetType() == typeof(User)) { \/\/ \u30e1\u30f3\u30d0\u30fc\u304c\u30e6\u30fc\u30b6\u306a\u3089\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3092\u30c1\u30a7\u30c3\u30af\n            User u = (User)m;\n            if (u.Mail == mail) {\n                return true;\n            }\n        } else if (m.GetType() == typeof(Group)) {\u3000\/\/ \u30b0\u30eb\u30fc\u30d7\u306a\u3089\u30b0\u30eb\u30fc\u30d7\u5185\u3092\u30c1\u30a7\u30c3\u30af\n            Group sg = (Group)m;\n            var grps = await graphClient.Groups.Request()\n                .WithUsernamePassword(username,secPwd)\n                .Expand(&quot;Members&quot;)\t\/\/ \u3053\u308c\u3092\u5165\u308c\u306a\u3044\u3068\u3001Members\u304c\u53d6\u308c\u306a\u3044\n                .Filter($&quot;id eq &#39;{sg.Id}&#39;&quot;)\t\/\/ ID\u3092\u6307\u5b9a\n                .GetAsync();\n            var grp = grps.FirstOrDefault();\n            if (grp != null) {\n\t\t\t\t\/\/ \u518d\u5e30\u547c\u51fa\u3057\n                return await IsMember(grp,mail,graphClient,username,secPwd);\n            } else {\n                return false;\n            }\n        }\n    }\n    return false;\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u30b0\u30eb\u30fc\u30d7\u6df1\u5ea6\u304c\u6df1\u304f\u306a\u308c\u3070\u3001\u4f55\u56de\u3082http\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3001\u52b9\u7387\u7684\u3067\u306f\u306a\u3044\u3068\u306f\u601d\u3046\u3051\u3069\u30fb\u30fb\u30fb<br>\u307e\u3041\u3001\u7d50\u679c\u306fOK\u3067\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3046\u3061\u306e\u793e\u5185\u30a2\u30d7\u30ea\u306e\u8a8d\u8a3c\u65b9\u6cd5\u306f\u4eca\u307e\u3067\u30d0\u30e9\u30d0\u30e9\u3067\u3001\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3068\u304b\u975e\u5e38\u306b\u5927\u5909\u3060\u3063\u305f\u306e &hellip; <a href=\"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=225\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,16,15,4],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-c","category-microsoft-graph","category-oauth2","category-4"],"_links":{"self":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225"}],"version-history":[{"count":5,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":231,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions\/231"}],"wp:attachment":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}