Skip to content

Commit

Permalink
Update for OIDC conformance. Closes auth0-samples#23
Browse files Browse the repository at this point in the history
  • Loading branch information
jerriep committed May 30, 2017
1 parent 911528b commit 9b79b73
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 134 deletions.
25 changes: 0 additions & 25 deletions Quickstart/03-Storing-Tokens/SampleMvcApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

Events = new OpenIdConnectEvents()
{
OnTicketReceived = context =>
{
// Get the ClaimsIdentity
var identity = context.Principal.Identity as ClaimsIdentity;
if (identity != null)
{
// Check if token names are stored in Properties
if (context.Properties.Items.ContainsKey(".TokenNames"))
{
// Token names a semicolon separated
string[] tokenNames = context.Properties.Items[".TokenNames"].Split(';');

// Add each token value as Claim
foreach (var tokenName in tokenNames)
{
// Tokens are stored in a Dictionary with the Key ".Token.<token name>"
string tokenValue = context.Properties.Items[$".Token.{tokenName}"];

identity.AddClaim(new Claim(tokenName, tokenValue));
}
}
}

return Task.FromResult(0);
},
// handle the logout redirection
OnRedirectToIdentityProviderForSignOut = (context) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IActionResult Profile()
{
return View(new UserProfileViewModel()
{
Name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value,
Name = User.Identity.Name,
EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value
});
Expand Down
41 changes: 7 additions & 34 deletions Quickstart/04-User-Profile/SampleMvcApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;

namespace SampleMvcApp
{
Expand Down Expand Up @@ -93,41 +94,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
// Configure the Claims Issuer to be Auth0
ClaimsIssuer = "Auth0",

// Saves tokens to the AuthenticationProperties
SaveTokens = true,
// Set the correct name claim type
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},

Events = new OpenIdConnectEvents
{
OnTicketReceived = context =>
{
// Get the ClaimsIdentity
var identity = context.Principal.Identity as ClaimsIdentity;
if (identity != null)
{
// Add the Name ClaimType. This is required if we want User.Identity.Name to actually return something!
if (!context.Principal.HasClaim(c => c.Type == ClaimTypes.Name) &&
identity.HasClaim(c => c.Type == "name"))
identity.AddClaim(new Claim(ClaimTypes.Name, identity.FindFirst("name").Value));

// Check if token names are stored in Properties
if (context.Properties.Items.ContainsKey(".TokenNames"))
{
// Token names a semicolon separated
string[] tokenNames = context.Properties.Items[".TokenNames"].Split(';');

// Add each token value as Claim
foreach (var tokenName in tokenNames)
{
// Tokens are stored in a Dictionary with the Key ".Token.<token name>"
string tokenValue = context.Properties.Items[$".Token.{tokenName}"];

identity.AddClaim(new Claim(tokenName, tokenValue));
}
}
}

return Task.CompletedTask;
},
// handle the logout redirection
OnRedirectToIdentityProviderForSignOut = (context) =>
{
Expand All @@ -154,9 +128,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
};
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("name");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("picture");
app.UseOpenIdConnectAuthentication(options);

app.UseMvc(routes =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public IActionResult Profile()
{
return View(new UserProfileViewModel()
{
Name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value,
Name = User.Identity.Name,
EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value,
Country = User.Claims.FirstOrDefault(c => c.Type == "country")?.Value
Country = User.Claims.FirstOrDefault(c => c.Type == "https://schemas.quickstarts.com/country")?.Value
});
}

Expand Down
42 changes: 7 additions & 35 deletions Quickstart/05-Rules/SampleMvcApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;

namespace SampleMvcApp
{
Expand Down Expand Up @@ -93,41 +94,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
// Configure the Claims Issuer to be Auth0
ClaimsIssuer = "Auth0",

// Saves tokens to the AuthenticationProperties
SaveTokens = true,
// Set the correct name claim type
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},

Events = new OpenIdConnectEvents
{
OnTicketReceived = context =>
{
// Get the ClaimsIdentity
var identity = context.Principal.Identity as ClaimsIdentity;
if (identity != null)
{
// Add the Name ClaimType. This is required if we want User.Identity.Name to actually return something!
if (!context.Principal.HasClaim(c => c.Type == ClaimTypes.Name) &&
identity.HasClaim(c => c.Type == "name"))
identity.AddClaim(new Claim(ClaimTypes.Name, identity.FindFirst("name").Value));

// Check if token names are stored in Properties
if (context.Properties.Items.ContainsKey(".TokenNames"))
{
// Token names a semicolon separated
string[] tokenNames = context.Properties.Items[".TokenNames"].Split(';');

// Add each token value as Claim
foreach (var tokenName in tokenNames)
{
// Tokens are stored in a Dictionary with the Key ".Token.<token name>"
string tokenValue = context.Properties.Items[$".Token.{tokenName}"];

identity.AddClaim(new Claim(tokenName, tokenValue));
}
}
}

return Task.CompletedTask;
},
// handle the logout redirection
OnRedirectToIdentityProviderForSignOut = (context) =>
{
Expand All @@ -154,10 +128,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
};
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("name");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("picture");
options.Scope.Add("country");
app.UseOpenIdConnectAuthentication(options);

app.UseMvc(routes =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public IActionResult Profile()
{
return View(new UserProfileViewModel()
{
Name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value,
Name = User.Identity.Name,
EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value,
Country = User.Claims.FirstOrDefault(c => c.Type == "country")?.Value
Country = User.Claims.FirstOrDefault(c => c.Type == "https://schemas.quickstarts.com/country")?.Value
});
}

Expand Down
42 changes: 7 additions & 35 deletions Quickstart/06-Authorization/SampleMvcApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;

namespace SampleMvcApp
{
Expand Down Expand Up @@ -93,41 +94,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
// Configure the Claims Issuer to be Auth0
ClaimsIssuer = "Auth0",

// Saves tokens to the AuthenticationProperties
SaveTokens = true,
// Set the correct name claim type
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},

Events = new OpenIdConnectEvents
{
OnTicketReceived = context =>
{
// Get the ClaimsIdentity
var identity = context.Principal.Identity as ClaimsIdentity;
if (identity != null)
{
// Add the Name ClaimType. This is required if we want User.Identity.Name to actually return something!
if (!context.Principal.HasClaim(c => c.Type == ClaimTypes.Name) &&
identity.HasClaim(c => c.Type == "name"))
identity.AddClaim(new Claim(ClaimTypes.Name, identity.FindFirst("name").Value));

// Check if token names are stored in Properties
if (context.Properties.Items.ContainsKey(".TokenNames"))
{
// Token names a semicolon separated
string[] tokenNames = context.Properties.Items[".TokenNames"].Split(';');

// Add each token value as Claim
foreach (var tokenName in tokenNames)
{
// Tokens are stored in a Dictionary with the Key ".Token.<token name>"
string tokenValue = context.Properties.Items[$".Token.{tokenName}"];

identity.AddClaim(new Claim(tokenName, tokenValue));
}
}
}

return Task.CompletedTask;
},
// handle the logout redirection
OnRedirectToIdentityProviderForSignOut = (context) =>
{
Expand All @@ -154,10 +128,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
};
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("name");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("picture");
options.Scope.Add("country");
options.Scope.Add("roles");
app.UseOpenIdConnectAuthentication(options);

Expand Down

0 comments on commit 9b79b73

Please sign in to comment.