-
Notifications
You must be signed in to change notification settings - Fork 21
Proposes more flexible AzureADOptions, and defaulting to AzureAD v2.0 #49
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,29 +30,47 @@ public class AzureADOptions | |
public string JwtBearerSchemeName { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the client Id. | ||
/// Gets or sets the client Id (Application Id) of the Azure AD application | ||
/// </summary> | ||
public string ClientId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the client secret. | ||
/// Gets or sets the audience for this Web Application or Web API (This audience needs | ||
/// to match the audience of the tokens sent to access this application) | ||
/// </summary> | ||
public string Audience { get; set; } = "api://{ClientId}"; | ||
|
||
/// <summary> | ||
/// Gets or sets the client secret for the application | ||
/// </summary> | ||
/// <remarks>The client secret is only used if the Web app or Web API calls a Web | ||
/// API</remarks> | ||
public string ClientSecret { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the tenant Id. | ||
/// Gets or sets the tenant. The tenant can have one of the following values: | ||
/// <list type="table"> | ||
/// <item><term>a tenant ID</term><description>A GUID representing the ID of the Azure Active Directory Tenant</description></item> | ||
/// <item><term>a domain</term><description>associated with Azure Active Directory</description></item> | ||
/// <item><term>common</term><description>if the <see cref="Authority"/> is Azure AD v2.0, enables to sign-in users from any | ||
/// Work and School account or Microsoft Personal Account. If Authority is Azure AD v1.0, enables sign-in from any Work and School accounts</description></item> | ||
/// <item><term>organizations</term><description>if the <see cref="Authority"/> is Azure AD v2.0, enables to sign-in users from any | ||
/// Work and School account</description></item> | ||
/// <item><term>consumers</term><description>if the <see cref="Authority"/> is Azure AD v2.0, enables to sign-in users from any | ||
/// Microsoft personal account</description></item> | ||
/// </list> | ||
/// </summary> | ||
public string TenantId { get; set; } | ||
public string Tenant { get; set; } = "common"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. It'll have to be discussed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should default to the common tenant here as just using the common tenant without additional per-app restriction is not recommended. |
||
|
||
/// <summary> | ||
/// Gets or sets the Azure Active Directory instance. | ||
/// </summary> | ||
public string Instance { get; set; } | ||
public string Instance { get; set; } = "https://login.microsoftonline.com"; | ||
|
||
/// <summary> | ||
/// Gets or sets the domain of the Azure Active Directory tennant. | ||
/// Azure AD Authority. | ||
/// </summary> | ||
public string Domain { get; set; } | ||
public string Authority { get; set; } = "https://{Instance}/{Tenant}/v2.0"; | ||
|
||
/// <summary> | ||
/// Gets or sets the sign in callback path. | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,8 +30,12 @@ public void Configure(string name, JwtBearerOptions options) | |||||
return; | ||||||
} | ||||||
|
||||||
options.Audience = azureADOptions.ClientId; | ||||||
options.Authority = new Uri(new Uri(azureADOptions.Instance), azureADOptions.TenantId).ToString(); | ||||||
string audienceFormat = azureADOptions.Authority.Replace("{ClientId}", "{0}"); | ||||||
options.Audience = string.Format(audienceFormat, azureADOptions.ClientId); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a single operation everywhere you do it.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't do things like this in the framework. Audience should be initialized here, not in the property, and it's also better to just use interpolated strings directly. |
||||||
|
||||||
string authorityFormat = azureADOptions.Authority.Replace("{Instance}", "{0}").Replace("{Tenant}", "{1}") ; | ||||||
options.Authority = string.Format(authorityFormat, azureADOptions.Instance, azureADOptions.Tenant); | ||||||
|
||||||
} | ||||||
|
||||||
public void Configure(JwtBearerOptions options) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,8 @@ public void Configure(string name, OpenIdConnectOptions options) | |
|
||
options.ClientId = azureADOptions.ClientId; | ||
options.ClientSecret = azureADOptions.ClientSecret; | ||
options.Authority = new Uri(new Uri(azureADOptions.Instance), azureADOptions.TenantId).ToString(); | ||
string authorityFormat = azureADOptions.Authority.Replace("{Instance}", "{0}").Replace("{Tenant}", "{1}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as for the audience |
||
options.Authority = string.Format(authorityFormat, azureADOptions.Instance, azureADOptions.Tenant); | ||
options.CallbackPath = azureADOptions.CallbackPath ?? options.CallbackPath; | ||
options.SignedOutCallbackPath = azureADOptions.SignedOutCallbackPath ?? options.SignedOutCallbackPath; | ||
options.SignInScheme = azureADOptions.CookieSchemeName; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,9 +35,8 @@ public async Task BearerAzureAD_Challenges_UnauthorizedRequests() | |
.AddAzureADBearer(o => | ||
{ | ||
o.Instance = "https://login.microsoftonline.com/"; | ||
o.Domain = "test.onmicrosoft.com"; | ||
o.Tenant= "test.onmicrosoft.com"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spacing |
||
o.ClientId = "ClientId"; | ||
o.TenantId = "TenantId"; | ||
}); | ||
|
||
services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, o => | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the audience be completely different from the client id or is always derived from the client id though some transformation.