Skip to content

Commit 8b1e6fd

Browse files
authored
Update OIDC sample (#45349)
1 parent 9d0487f commit 8b1e6fd

File tree

1 file changed

+24
-4
lines changed
  • src/Security/Authentication/OpenIdConnect/samples/OpenIdConnectSample

1 file changed

+24
-4
lines changed

src/Security/Authentication/OpenIdConnect/samples/OpenIdConnectSample/Startup.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
using System.Globalization;
55
using System.Net.Http;
6+
using System.Security.Claims;
67
using System.Text.Encodings.Web;
78
using System.Text.Json;
89
using Microsoft.AspNetCore.Authentication;
910
using Microsoft.AspNetCore.Authentication.Cookies;
11+
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
1012
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
1113
using Microsoft.Extensions.Options;
1214
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
@@ -102,15 +104,18 @@ public void ConfigureServices(IServiceCollection services)
102104
o.Authority = Configuration["oidc:authority"];
103105
*/
104106
// https://github.com/IdentityServer/IdentityServer4.Demo/blob/master/src/IdentityServer4Demo/Config.cs
105-
o.ClientId = "hybrid";
107+
o.ClientId = "interactive.confidential";
106108
o.ClientSecret = "secret"; // for code flow
107-
o.Authority = "https://demo.identityserver.io/";
109+
o.Authority = "https://demo.duendesoftware.com/";
108110

109-
o.ResponseType = OpenIdConnectResponseType.CodeIdToken;
111+
o.ResponseType = OpenIdConnectResponseType.Code;
110112
o.SaveTokens = true;
111113
o.GetClaimsFromUserInfoEndpoint = true;
112114
o.AccessDeniedPath = "/access-denied-from-remote";
113-
o.MapInboundClaims = false;
115+
// o.MapInboundClaims = false;
116+
o.ClaimsIssuer = "MyCustomIssuer";
117+
118+
o.ClaimActions.Add(new IssuerFixupAction());
114119

115120
// o.ClaimActions.MapAllExcept("aud", "iss", "iat", "nbf", "exp", "aio", "c_hash", "uti", "nonce");
116121

@@ -352,5 +357,20 @@ private static async Task WriteTableHeader(HttpResponse response, IEnumerable<st
352357

353358
private static string HtmlEncode(string content) =>
354359
string.IsNullOrEmpty(content) ? string.Empty : HtmlEncoder.Default.Encode(content);
360+
361+
private class IssuerFixupAction : ClaimAction
362+
{
363+
public IssuerFixupAction() : base(ClaimTypes.NameIdentifier, string.Empty) { }
364+
365+
public override void Run(JsonElement userData, ClaimsIdentity identity, string issuer)
366+
{
367+
var oldClaims = identity.Claims.ToList();
368+
foreach (var claim in oldClaims)
369+
{
370+
identity.RemoveClaim(claim);
371+
identity.AddClaim(new Claim(claim.Type, claim.Value, claim.ValueType, issuer, claim.OriginalIssuer, claim.Subject));
372+
}
373+
}
374+
}
355375
}
356376

0 commit comments

Comments
 (0)