Skip to content

Commit 92a0966

Browse files
authored
OIDC Blazor re-use existing schemes (dotnet#259)
1 parent 9d0e1e4 commit 92a0966

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

8.0/BlazorWebAppOidc/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute
1818
// the user will automatically be signed back in the next time they visit a page that requires authentication
1919
// without being able to choose another account.
2020
group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl),
21-
["Cookies", "MicrosoftOidc"]));
21+
[CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"]));
2222

2323
return group;
2424
}

8.0/BlazorWebAppOidc/BlazorWebAppOidc/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
using Microsoft.AspNetCore.Mvc;
88
using BlazorWebAppOidc.Weather;
99
using BlazorWebAppOidc.Client.Weather;
10+
11+
const string MS_OIDC_SCHEME = "MicrosoftOidc";
1012

1113
var builder = WebApplication.CreateBuilder(args);
1214

1315
// Add services to the container.
14-
builder.Services.AddAuthentication("MicrosoftOidc")
15-
.AddOpenIdConnect("MicrosoftOidc", oidcOptions =>
16+
builder.Services.AddAuthentication(MS_OIDC_SCHEME)
17+
.AddOpenIdConnect(MS_OIDC_SCHEME, oidcOptions =>
1618
{
1719
// For the following OIDC settings, any line that's commented out
1820
// represents a DEFAULT setting. If you adopt the default, you can
@@ -133,14 +135,14 @@
133135
// expiration.
134136
// ........................................................................
135137
})
136-
.AddCookie("Cookies");
138+
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
137139

138140
// ConfigureCookieOidcRefresh attaches a cookie OnValidatePrincipal callback to get
139141
// a new access token when the current one expires, and reissue a cookie with the
140142
// new access token saved inside. If the refresh fails, the user will be signed
141143
// out. OIDC connect options are set for saving tokens and the offline access
142144
// scope.
143-
builder.Services.ConfigureCookieOidcRefresh("Cookies", "MicrosoftOidc");
145+
builder.Services.ConfigureCookieOidcRefresh(CookieAuthenticationDefaults.AuthenticationScheme, MS_OIDC_SCHEME);
144146

145147
builder.Services.AddAuthorization();
146148

8.0/BlazorWebAppOidcBff/BlazorWebAppOidc/LoginLogoutEndpointRouteBuilderExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.AspNetCore.Authentication;
22
using Microsoft.AspNetCore.Authentication.Cookies;
3-
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
43
using Microsoft.AspNetCore.Mvc;
54

65
namespace Microsoft.AspNetCore.Routing;
@@ -18,7 +17,7 @@ internal static IEndpointConventionBuilder MapLoginAndLogout(this IEndpointRoute
1817
// the user will automatically be signed back in the next time they visit a page that requires authentication
1918
// without being able to choose another account.
2019
group.MapPost("/logout", ([FromForm] string? returnUrl) => TypedResults.SignOut(GetAuthProperties(returnUrl),
21-
["Cookies", "MicrosoftOidc"]));
20+
[CookieAuthenticationDefaults.AuthenticationScheme, "MicrosoftOidc"]));
2221

2322
return group;
2423
}

8.0/BlazorWebAppOidcBff/BlazorWebAppOidc/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
using BlazorWebAppOidc.Components;
88
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
99
using Microsoft.AspNetCore.Authentication.Cookies;
10+
11+
const string MS_OIDC_SCHEME = "MicrosoftOidc";
1012

1113
var builder = WebApplication.CreateBuilder(args);
1214

1315
// Add service defaults & Aspire components.
1416
builder.AddServiceDefaults();
1517

1618
// Add services to the container.
17-
builder.Services.AddAuthentication("MicrosoftOidc")
18-
.AddOpenIdConnect("MicrosoftOidc", oidcOptions =>
19+
builder.Services.AddAuthentication(MS_OIDC_SCHEME)
20+
.AddOpenIdConnect(MS_OIDC_SCHEME, oidcOptions =>
1921
{
2022
// For the following OIDC settings, any line that's commented out
2123
// represents a DEFAULT setting. If you adopt the default, you can
@@ -151,14 +153,14 @@
151153
// expiration.
152154
// ........................................................................
153155
})
154-
.AddCookie("Cookies");
156+
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
155157

156158
// ConfigureCookieOidcRefresh attaches a cookie OnValidatePrincipal callback to get
157159
// a new access token when the current one expires, and reissue a cookie with the
158160
// new access token saved inside. If the refresh fails, the user will be signed
159161
// out. OIDC connect options are set for saving tokens and the offline access
160162
// scope.
161-
builder.Services.ConfigureCookieOidcRefresh("Cookies", "MicrosoftOidc");
163+
builder.Services.ConfigureCookieOidcRefresh(CookieAuthenticationDefaults.AuthenticationScheme, MS_OIDC_SCHEME);
162164

163165
builder.Services.AddAuthorization();
164166

0 commit comments

Comments
 (0)