forked from AzureAD/microsoft-identity-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicrosoftGraphExtensions.cs
More file actions
123 lines (112 loc) · 5.66 KB
/
MicrosoftGraphExtensions.cs
File metadata and controls
123 lines (112 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Graph;
using Microsoft.Identity.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;
namespace Microsoft.Identity.Web
{
#if !NET472 && !NET462 && !NETSTANDARD2_0
/// <summary>
/// Extensions methods on a MicrosoftIdentityAppCallingWebApiAuthenticationBuilder builder
/// to add support to call Microsoft Graph.
/// </summary>
public static class MicrosoftGraphExtensions
{
/// <summary>
/// Add support to call Microsoft Graph. From a named option and a configuration section.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="configurationSection">Configuration section.</param>
/// <returns>The builder to chain.</returns>
[RequiresUnreferencedCode("Calls Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object).")]
[RequiresDynamicCode("Calls Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object).")]
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraph(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
IConfigurationSection configurationSection)
{
return builder.AddMicrosoftGraph(
options => configurationSection.Bind(options));
}
/// <summary>
/// Add support to call Microsoft Graph. From a base Graph URL and a default scope.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="graphBaseUrl">Named instance of option.</param>
/// <param name="defaultScopes">Configuration section.</param>
/// <returns>The builder to chain.</returns>
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraph(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
string graphBaseUrl = Constants.GraphBaseUrlV1,
IEnumerable<string>? defaultScopes = null)
{
return builder.AddMicrosoftGraph(
options =>
{
options.BaseUrl = graphBaseUrl;
options.Scopes = defaultScopes ?? new List<string> { Constants.UserReadScope };
});
}
/// <summary>
/// Add support to call Microsoft Graph. From a named options and a configuration method.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="configureMicrosoftGraphOptions">Method to configure the options.</param>
/// <returns>The builder to chain.</returns>
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraph(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
Action<GraphServiceClientOptions> configureMicrosoftGraphOptions)
{
_ = Throws.IfNull(builder);
builder.Services.AddMicrosoftGraph(configureMicrosoftGraphOptions);
return builder;
}
/// <summary>
/// Add support to call Microsoft Graph.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="graphServiceClientFactory">Function to create a GraphServiceClient.</param>
/// <param name="initialScopes">Initial scopes.</param>
/// <returns>The builder to chain.</returns>
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraph(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
Func<IAuthenticationProvider, GraphServiceClient> graphServiceClientFactory, IEnumerable<string> initialScopes)
{
_ = Throws.IfNull(builder);
builder.Services.AddScoped<GraphServiceClient, GraphServiceClient>(serviceProvider =>
{
IAuthorizationHeaderProvider authorizationHeaderProvider = serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>();
return graphServiceClientFactory(new GraphAuthenticationProvider(
authorizationHeaderProvider,
new GraphAuthenticationOptions() { Scopes = initialScopes.ToArray() }));
});
return builder;
}
/// <summary>
/// Add support to call Microsoft Graph.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="graphServiceClientFactory">Function to create a GraphServiceClient.</param>
/// <returns>The builder to chain.</returns>
public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraphAppOnly(
this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
Func<IAuthenticationProvider, GraphServiceClient> graphServiceClientFactory)
{
_ = Throws.IfNull(builder);
builder.Services.AddScoped<GraphServiceClient, GraphServiceClient>(serviceProvider =>
{
IAuthorizationHeaderProvider authorizationHeaderProvider = serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>();
return graphServiceClientFactory(new GraphAuthenticationProvider(
authorizationHeaderProvider,
new GraphAuthenticationOptions() { RequestAppToken = true }));
});
return builder;
}
}
#endif
}