Skip to content

Commit da885b5

Browse files
committed
Add both use OpenId and Custom authorization sample code for dashboard.
1 parent 1a3465e commit da885b5

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

samples/Sample.Dashboard.Auth/MyDashboardAuthenticationHandler.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text.Encodings.Web;
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Authentication;
6+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
67
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.Options;
89

@@ -23,22 +24,25 @@ public class MyDashboardAuthenticationHandler : AuthenticationHandler<MyDashboar
2324
public MyDashboardAuthenticationHandler(IOptionsMonitor<MyDashboardAuthenticationSchemeOptions> options,
2425
ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder)
2526
{
26-
options.CurrentValue.ForwardChallenge = "";
27+
// options.CurrentValue.ForwardChallenge = "";
2728
}
2829

2930
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
3031
{
3132
var testAuthHeaderPresent = Request.Headers["X-Base-Token"].Contains("xxx");
3233

3334
var authResult = testAuthHeaderPresent ? CreateAuthenticatonTicket() : AuthenticateResult.NoResult();
34-
35+
3536
return Task.FromResult(authResult);
3637
}
3738

3839
protected override Task HandleChallengeAsync(AuthenticationProperties properties)
3940
{
40-
Response.Headers["WWW-Authenticate"] = MyDashboardAuthenticationSchemeDefaults.Scheme;
41-
return base.HandleChallengeAsync(properties);
41+
//Response.Headers["WWW-Authenticate"] = MyDashboardAuthenticationSchemeDefaults.Scheme;
42+
//return base.HandleChallengeAsync(properties);
43+
44+
// Challenge use OpenId for AddCapWithOpenIdAndCustomAuthorization
45+
return Context.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, properties);
4246
}
4347

4448
private AuthenticateResult CreateAuthenticatonTicket()

samples/Sample.Dashboard.Auth/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
"Sample.Dashboard.Auth": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22+
"launchUrl": "cap",
2223
"environmentVariables": {
2324
"ASPNETCORE_ENVIRONMENT": "Development"
2425
},
25-
"applicationUrl": "https://localhost:5001"
26+
"applicationUrl": "https://localhost:5001/"
2627
}
2728
}
2829
}

samples/Sample.Dashboard.Auth/Startup.cs

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ public class Startup
1010
{
1111
public void ConfigureServices(IServiceCollection services)
1212
{
13-
AddCapWithOpenIdAuthorization(services);
13+
// AddCapWithOpenIdAuthorization(services);
1414
// AddCapWithAnonymousAccess(services);
1515
// AddCapWithCustomAuthorization(services);
16-
16+
AddCapWithOpenIdAndCustomAuthorization(services);
17+
1718
services.AddCors(x =>
1819
{
1920
x.AddDefaultPolicy(p =>
@@ -41,10 +42,10 @@ public void Configure(IApplicationBuilder app)
4142
private IServiceCollection AddCapWithOpenIdAuthorization(IServiceCollection services)
4243
{
4344
const string DashboardAuthorizationPolicy = "DashboardAuthorizationPolicy";
44-
45+
4546
services
4647
.AddAuthorization(options =>
47-
{
48+
{
4849
options.AddPolicy(DashboardAuthorizationPolicy, policy => policy
4950
.AddAuthenticationSchemes(OpenIdConnectDefaults.AuthenticationScheme)
5051
.RequireAuthenticatedUser());
@@ -64,11 +65,12 @@ private IServiceCollection AddCapWithOpenIdAuthorization(IServiceCollection serv
6465
options.Scope.Add("openid");
6566
options.Scope.Add("profile");
6667
});
67-
68+
6869
services.AddCap(cap =>
6970
{
7071
cap.UseDashboard(d =>
7172
{
73+
d.AllowAnonymousExplicit = false;
7274
d.AuthorizationPolicy = DashboardAuthorizationPolicy;
7375
});
7476
cap.UseInMemoryStorage();
@@ -77,21 +79,21 @@ private IServiceCollection AddCapWithOpenIdAuthorization(IServiceCollection serv
7779

7880
return services;
7981
}
80-
82+
8183
private IServiceCollection AddCapWithCustomAuthorization(IServiceCollection services)
8284
{
8385
const string MyDashboardAuthenticationPolicy = "MyDashboardAuthenticationPolicy";
84-
86+
8587
services
8688
.AddAuthorization(options =>
87-
{
89+
{
8890
options.AddPolicy(MyDashboardAuthenticationPolicy, policy => policy
8991
.AddAuthenticationSchemes(MyDashboardAuthenticationSchemeDefaults.Scheme)
9092
.RequireAuthenticatedUser());
9193
})
9294
.AddAuthentication()
93-
.AddScheme<MyDashboardAuthenticationSchemeOptions, MyDashboardAuthenticationHandler>(MyDashboardAuthenticationSchemeDefaults.Scheme,null);
94-
95+
.AddScheme<MyDashboardAuthenticationSchemeOptions, MyDashboardAuthenticationHandler>(MyDashboardAuthenticationSchemeDefaults.Scheme, null);
96+
9597
services.AddCap(cap =>
9698
{
9799
cap.UseDashboard(d =>
@@ -104,7 +106,49 @@ private IServiceCollection AddCapWithCustomAuthorization(IServiceCollection serv
104106

105107
return services;
106108
}
107-
109+
110+
private IServiceCollection AddCapWithOpenIdAndCustomAuthorization(IServiceCollection services)
111+
{
112+
const string DashboardAuthorizationPolicy = "DashboardAuthorizationPolicy";
113+
114+
services
115+
.AddAuthorization(options =>
116+
{
117+
options.AddPolicy(DashboardAuthorizationPolicy, policy => policy
118+
.AddAuthenticationSchemes(OpenIdConnectDefaults.AuthenticationScheme, MyDashboardAuthenticationSchemeDefaults.Scheme)
119+
.RequireAuthenticatedUser());
120+
})
121+
.AddAuthentication(opt => opt.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme)
122+
.AddScheme<MyDashboardAuthenticationSchemeOptions, MyDashboardAuthenticationHandler>(MyDashboardAuthenticationSchemeDefaults.Scheme, null)
123+
.AddCookie()
124+
.AddOpenIdConnect(options =>
125+
{
126+
options.RequireHttpsMetadata = false;
127+
options.Authority = "https://demo.duendesoftware.com/";
128+
options.ClientId = "interactive.confidential";
129+
options.ClientSecret = "secret";
130+
options.ResponseType = "code";
131+
options.UsePkce = true;
132+
133+
options.Scope.Clear();
134+
options.Scope.Add("openid");
135+
options.Scope.Add("profile");
136+
});
137+
138+
services.AddCap(cap =>
139+
{
140+
cap.UseDashboard(d =>
141+
{
142+
d.AllowAnonymousExplicit = false;
143+
d.AuthorizationPolicy = DashboardAuthorizationPolicy;
144+
});
145+
cap.UseInMemoryStorage();
146+
cap.UseInMemoryMessageQueue();
147+
});
148+
149+
return services;
150+
}
151+
108152
private IServiceCollection AddCapWithAnonymousAccess(IServiceCollection services)
109153
{
110154
services.AddCap(cap =>

0 commit comments

Comments
 (0)