I only have one code file \u2013 Program.cs ( the default code file for a console app ). Here is the code:<\/p>\n\n\n
using Microsoft.Graph;\nusing Microsoft.Graph.Auth;\nusing Microsoft.Identity.Client;\nusing System;\nusing System.Threading.Tasks;\n\nnamespace GraphClient\n{\n class Program\n {\n static String client_id = \"{client_id}\";\n static String tenant_id = \"{tenant_id}\";\n static String[] scopes = { \"https:\/\/graph.microsoft.com\/.default\" };\n\n static IPublicClientApplication _pca = null;\n private static IPublicClientApplication Pca\n {\n get\n {\n if(_pca == null )\n {\n _pca = PublicClientApplicationBuilder\n .Create( client_id )\n .WithTenantId( tenant_id )\n .Build();\n }\n\n return _pca;\n }\n }\n\n static InteractiveAuthenticationProvider _authProvider = null;\n private static InteractiveAuthenticationProvider AuthProvider\n {\n get\n {\n if(_authProvider == null )\n {\n _authProvider = new InteractiveAuthenticationProvider( Pca, scopes );\n }\n return _authProvider;\n }\n }\n\n static GraphServiceClient _graphClient = null;\n private static GraphServiceClient GraphClient\n {\n get\n {\n if(_graphClient == null )\n {\n _graphClient = new GraphServiceClient( AuthProvider );\n }\n return _graphClient;\n }\n }\n\n static void Main(string[] args)\n {\n Get_Me().Wait();\n\n Console.WriteLine( $\"\\nPress any key to close...\" );\n Console.ReadKey();\n }\n\n static async Task Get_Me()\n {\n User user = null;\n\n user = await GraphClient.Me.Request().GetAsync();\n\n Console.WriteLine( $\"Display Name = {user.DisplayName}\\nEmail = {user.Mail}\" );\n }\n }\n}\n<\/pre>\n <\/p>\n\n\n
To implement one of the other flows, you may be modifying the Client type ( line 15 in the code file above ) based on whether or not it is a confidential client or a public client ( see list at the beginning of this post ) and the property for it just below, and line 39 ( code file above) where we are setting the auth provider as well as the underlying member for that. So, for example, a confidential client using the client credentials flow would look like this in the same code file:<\/p>\n\n\n
static String client_id = \"{client_id}\";\n static String tenant_id = \"{tenant_id}\";\n static String client_secret = \"{client_secret}\";\n static String[] scopes = { \"https:\/\/graph.microsoft.com\/.default\" };\n static IConfidentialClientApplication _pca = null;\n private static IConfidentialClientApplication Pca\n {\n get\n {\n if(_pca == null )\n {\n _pca = ConfidentialClientApplicationBuilder\n .Create( client_id )\n .WithTenantId( tenant_id )\n .WithClientSecret( client_secret )\n .Build();\n }\n return _pca;\n }\n }\n static ClientCredentialProvider _authProvider = null;\n private static ClientCredentialProvider AuthProvider\n {\n get\n {\n if(_authProvider == null )\n {\n _authProvider = new ClientCredentialProvider( Pca );\n }\n return _authProvider;\n }\n }<\/pre>\n <\/p>","protected":false},"excerpt":{"rendered":"
The Graph Client Authentication Providers allows for each authentication to the graph endpoint implementing a variety of OAUTH2 flows. I will demonstrate the use of this library in c# code based on this GitHub. Previously, you had to build your own Authentication Provider ( see my creation of the client credentials provider in a vb.net application here ) . This library will allow you to use the following flows: Confidential…<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,11],"tags":[193,192],"class_list":["post-6895","post","type-post","status-publish","format-standard","hentry","category-authentication-flows","category-ms-graph-client","tag-graphclient","tag-iauthenticationprovider"],"_links":{"self":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/posts\/6895"}],"collection":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/comments?post=6895"}],"version-history":[{"count":25,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/posts\/6895\/revisions"}],"predecessor-version":[{"id":6920,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/posts\/6895\/revisions\/6920"}],"wp:attachment":[{"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/media?parent=6895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/categories?post=6895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.aaddevsup.xyz\/wp-json\/wp\/v2\/tags?post=6895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}