-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# WebApp | ||
|
||
<<< @/../samples/WebApp/Program.cs | ||
|
||
<<< @/../samples/WebApp/Controllers/AccountController.cs | ||
|
||
See sample source code: [keycloak-authorization-services-dotnet/tree/main/samples/WebApp](https://github.com/NikiforovAll/keycloak-authorization-services-dotnet/tree/main/samples/WebApp) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace WebApp_OpenIDConnect_DotNet.Controllers; | ||
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-windows-latest
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-windows-latest
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-windows-latest
Check warning on line 1 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-windows-latest
|
||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authentication.OpenIdConnect; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
public class AccountController : Controller | ||
{ | ||
public IActionResult SignIn() | ||
{ | ||
if (!User.Identity.IsAuthenticated) | ||
Check warning on line 14 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 14 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 14 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 14 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 14 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-windows-latest
Check warning on line 14 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-windows-latest
Check warning on line 14 in samples/WebApp/Controllers/AccountController.cs GitHub Actions / Build-windows-latest
|
||
{ | ||
return this.Challenge(OpenIdConnectDefaults.AuthenticationScheme); | ||
} | ||
|
||
return this.RedirectToAction("Index", "Home"); | ||
} | ||
|
||
[Authorize] | ||
public async Task<IActionResult> SignOutAsync() | ||
{ | ||
var idToken = await this.HttpContext.GetTokenAsync("id_token"); | ||
|
||
return this.SignOut( | ||
new AuthenticationProperties | ||
{ | ||
RedirectUri = "/", | ||
Items = { { "id_token_hint", idToken } } | ||
}, | ||
CookieAuthenticationDefaults.AuthenticationScheme, | ||
OpenIdConnectDefaults.AuthenticationScheme | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace WebApp_OpenIDConnect_DotNet.Controllers; | ||
|
||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using WebApp_OpenIDConnect_DotNet.Models; | ||
|
||
[Authorize] | ||
public class HomeController : Controller | ||
{ | ||
public IActionResult Index() => this.View(); | ||
|
||
public IActionResult Privacy() => this.View(); | ||
|
||
[AllowAnonymous] | ||
public IActionResult Public() => this.View(); | ||
|
||
[AllowAnonymous] | ||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
public IActionResult Error() => | ||
this.View( | ||
new ErrorViewModel | ||
{ | ||
RequestId = Activity.Current?.Id ?? this.HttpContext.TraceIdentifier | ||
} | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"clientId": "test-client", | ||
"name": "", | ||
"description": "", | ||
"rootUrl": "", | ||
"adminUrl": "", | ||
"baseUrl": "", | ||
"surrogateAuthRequired": false, | ||
"enabled": true, | ||
"alwaysDisplayInConsole": false, | ||
"clientAuthenticatorType": "client-secret", | ||
"redirectUris": [ | ||
"*" | ||
], | ||
"webOrigins": [ | ||
"*" | ||
], | ||
"notBefore": 0, | ||
"bearerOnly": false, | ||
"consentRequired": false, | ||
"standardFlowEnabled": true, | ||
"implicitFlowEnabled": true, | ||
"directAccessGrantsEnabled": true, | ||
"serviceAccountsEnabled": false, | ||
"publicClient": true, | ||
"frontchannelLogout": true, | ||
"protocol": "openid-connect", | ||
"attributes": { | ||
"oidc.ciba.grant.enabled": "false", | ||
"backchannel.logout.session.required": "false", | ||
"post.logout.redirect.uris": "*", | ||
"oauth2.device.authorization.grant.enabled": "false", | ||
"display.on.consent.screen": "false", | ||
"backchannel.logout.revoke.offline.tokens": "false" | ||
}, | ||
"authenticationFlowBindingOverrides": {}, | ||
"fullScopeAllowed": true, | ||
"nodeReRegistrationTimeout": -1, | ||
"protocolMappers": [ | ||
{ | ||
"name": "realm_access", | ||
"protocol": "openid-connect", | ||
"protocolMapper": "oidc-usermodel-realm-role-mapper", | ||
"consentRequired": false, | ||
"config": { | ||
"introspection.token.claim": "true", | ||
"multivalued": "true", | ||
"userinfo.token.claim": "true", | ||
"id.token.claim": "true", | ||
"lightweight.claim": "false", | ||
"access.token.claim": "true", | ||
"claim.name": "realm_roles", | ||
"jsonType.label": "String" | ||
} | ||
} | ||
], | ||
"defaultClientScopes": [ | ||
"web-origins", | ||
"acr", | ||
"profile", | ||
"roles", | ||
"email" | ||
], | ||
"optionalClientScopes": [ | ||
"address", | ||
"phone", | ||
"offline_access", | ||
"microprofile-jwt" | ||
], | ||
"access": { | ||
"view": true, | ||
"configure": true, | ||
"manage": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace WebApp_OpenIDConnect_DotNet.Models | ||
{ | ||
public class ErrorViewModel | ||
{ | ||
public string RequestId { get; set; } | ||
Check warning on line 5 in samples/WebApp/Models/ErrorViewModel.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 5 in samples/WebApp/Models/ErrorViewModel.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 5 in samples/WebApp/Models/ErrorViewModel.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 5 in samples/WebApp/Models/ErrorViewModel.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 5 in samples/WebApp/Models/ErrorViewModel.cs GitHub Actions / Build-windows-latest
Check warning on line 5 in samples/WebApp/Models/ErrorViewModel.cs GitHub Actions / Build-windows-latest
Check warning on line 5 in samples/WebApp/Models/ErrorViewModel.cs GitHub Actions / Build-windows-latest
|
||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Keycloak.AuthServices.Authentication; | ||
using Keycloak.AuthServices.Authorization.AuthorizationServer; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.OpenIdConnect; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.Configure<CookiePolicyOptions>(options => | ||
{ | ||
options.CheckConsentNeeded = context => true; | ||
options.MinimumSameSitePolicy = SameSiteMode.Unspecified; | ||
}); | ||
|
||
builder | ||
.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) | ||
.AddKeycloakWebApp( | ||
builder.Configuration.GetSection(KeycloakAuthorizationServerOptions.Section), | ||
configureOpenIdConnectOptions: options => | ||
{ | ||
// used for front-channel logout | ||
options.SaveTokens = true; | ||
options.Events = new OpenIdConnectEvents | ||
{ | ||
OnSignedOutCallbackRedirect = context => | ||
{ | ||
context.Response.Redirect("/Home/Public"); | ||
context.HandleResponse(); | ||
return Task.CompletedTask; | ||
} | ||
}; | ||
// NOTE, the source for claims is id_token and not access token. | ||
// By default, id_token doesn't contain realm_roles claim | ||
// and you will need to create a mapper for that | ||
options.ClaimActions.MapUniqueJsonKey("realm_access", "realm_access"); | ||
} | ||
); | ||
|
||
builder.Services.PostConfigure<OpenIdConnectOptions>(options => { }); | ||
|
||
builder.Services.AddControllersWithViews(); | ||
|
||
builder.Services.AddRazorPages(); | ||
|
||
var app = builder.Build(); | ||
|
||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
app.UseExceptionHandler("/Home/Error"); | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
app.UseCookiePolicy(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}") | ||
.RequireAuthorization(); | ||
app.MapRazorPages(); | ||
|
||
app.Run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "https://localhost:44321/", | ||
"sslPort": 44321 | ||
} | ||
}, | ||
"profiles": { | ||
"WebApp_OpenIDConnect_DotNet": { | ||
"commandName": "Project", | ||
"launchBrowser": false, | ||
"applicationUrl": "https://localhost:44321;http://localhost:3110", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"secrets1": { | ||
"type": "secrets" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"secrets1": { | ||
"type": "secrets.user" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# An ASP.NET Core Web app signing-in users with Keycloak.AuthServices | ||
|
||
## Scenario | ||
|
||
This sample shows how to build a .NET Core MVC Web app that uses OpenID Connect to sign in users. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@{ | ||
ViewData["Title"] = "Home Page"; | ||
} | ||
|
||
<h1> | ||
ASP.NET Core web app signing-in users in your organization | ||
</h1> | ||
<p> | ||
This sample shows how to build a .NET Core MVC Web app that uses OpenID Connect to sign in users in your | ||
organization. | ||
</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@{ | ||
ViewData["Title"] = "Privacy Policy"; | ||
} | ||
<h2>@ViewData["Title"]</h2> | ||
|
||
<p>Use this page to detail your site's privacy policy.</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@{ | ||
ViewData["Title"] = "Public Page"; | ||
} | ||
<h2>@ViewData["Title"]</h2> | ||
|
||
<p>Public Page</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@model ErrorViewModel | ||
@{ | ||
ViewData["Title"] = "Error"; | ||
} | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (Model.ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> |