Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloned Velusia to Rentor for customized class sample & integer TKey #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ target/
.nuget/
.vs/
.build/
.dotnet/

# User-specific files
*.suo
Expand Down
19 changes: 18 additions & 1 deletion OpenIddict.Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Weytta", "Weytta", "{374481
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Weytta.Client", "samples\Weytta\Weytta.Client\Weytta.Client.csproj", "{20C5B39E-CEBA-4CCD-979A-5499B4030043}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weytta.Server", "samples\Weytta\Weytta.Server\Weytta.Server.csproj", "{02D5D1C5-675D-482D-9F74-F06FC522D2CC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Weytta.Server", "samples\Weytta\Weytta.Server\Weytta.Server.csproj", "{02D5D1C5-675D-482D-9F74-F06FC522D2CC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Rentor", "Rentor", "{F549911A-B043-453F-938C-06882ADEA987}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rentor.Client", "samples\Rentor\Rentor.Client\Rentor.Client.csproj", "{A55B0597-7E77-4FBA-8C9E-48842EC3B48A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rentor.Server", "samples\Rentor\Rentor.Server\Rentor.Server.csproj", "{373EB5EA-375B-4343-A3E0-FD5609795F90}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -170,6 +176,14 @@ Global
{02D5D1C5-675D-482D-9F74-F06FC522D2CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02D5D1C5-675D-482D-9F74-F06FC522D2CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02D5D1C5-675D-482D-9F74-F06FC522D2CC}.Release|Any CPU.Build.0 = Release|Any CPU
{A55B0597-7E77-4FBA-8C9E-48842EC3B48A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A55B0597-7E77-4FBA-8C9E-48842EC3B48A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A55B0597-7E77-4FBA-8C9E-48842EC3B48A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A55B0597-7E77-4FBA-8C9E-48842EC3B48A}.Release|Any CPU.Build.0 = Release|Any CPU
{373EB5EA-375B-4343-A3E0-FD5609795F90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{373EB5EA-375B-4343-A3E0-FD5609795F90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{373EB5EA-375B-4343-A3E0-FD5609795F90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{373EB5EA-375B-4343-A3E0-FD5609795F90}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -202,6 +216,9 @@ Global
{37448199-E002-4FA0-A712-B6DB22DA98EC} = {8B467944-153B-4C90-BAB1-8F1B34C3075A}
{20C5B39E-CEBA-4CCD-979A-5499B4030043} = {37448199-E002-4FA0-A712-B6DB22DA98EC}
{02D5D1C5-675D-482D-9F74-F06FC522D2CC} = {37448199-E002-4FA0-A712-B6DB22DA98EC}
{F549911A-B043-453F-938C-06882ADEA987} = {8B467944-153B-4C90-BAB1-8F1B34C3075A}
{A55B0597-7E77-4FBA-8C9E-48842EC3B48A} = {F549911A-B043-453F-938C-06882ADEA987}
{373EB5EA-375B-4343-A3E0-FD5609795F90} = {F549911A-B043-453F-938C-06882ADEA987}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F3ECDD26-F40D-4AB4-BC48-8DF143F98FAE}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Mvc;

namespace Rentor.Client.Controllers
{
public class AuthenticationController : Controller
{
[HttpGet("~/login")]
public ActionResult LogIn()
{
return Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme);
}

[HttpGet("~/logout"), HttpPost("~/logout")]
public ActionResult LogOut()
{
// is redirected from the identity provider after a successful authorization flow and
// to redirect the user agent to the identity provider to sign out.
return SignOut(CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme);
}
}
}
45 changes: 45 additions & 0 deletions samples/Rentor/Rentor.Client/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;

namespace Rentor.Client.Controllers
{
public class HomeController : Controller
{
private readonly IHttpClientFactory _httpClientFactory;

public HomeController(IHttpClientFactory httpClientFactory)
=> _httpClientFactory = httpClientFactory;

[HttpGet("~/")]
public ActionResult Index() => View("Home");

[Authorize, HttpPost("~/")]
public async Task<ActionResult> Index(CancellationToken cancellationToken)
{
var token = await HttpContext.GetTokenAsync(CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectParameterNames.AccessToken);
if (string.IsNullOrEmpty(token))
{
throw new InvalidOperationException("The access token cannot be found in the authentication ticket. " +
"Make sure that SaveTokens is set to true in the OIDC options.");
}

using var client = _httpClientFactory.CreateClient();

using var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:44213/api/message");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

using var response = await client.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();

return View("Home", model: await response.Content.ReadAsStringAsync());
}
}
}
15 changes: 15 additions & 0 deletions samples/Rentor/Rentor.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Rentor.Client
{
public static class Program
{
public static void Main(string[] args) =>
CreateHostBuilder(args).Build().Run();

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>());
}
}
27 changes: 27 additions & 0 deletions samples/Rentor/Rentor.Client/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53507/",
"sslPort": 44238
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Rentor.Client": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:44238/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
11 changes: 11 additions & 0 deletions samples/Rentor/Rentor.Client/Rentor.Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
</ItemGroup>

</Project>
81 changes: 81 additions & 0 deletions samples/Rentor/Rentor.Client/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;

namespace Rentor.Client
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})

.AddCookie(options =>
{
options.LoginPath = "/login";
})

.AddOpenIdConnect(options =>
{
// Note: these settings must match the application details
// inserted in the database at the server level.
options.ClientId = "mvc";
options.ClientSecret = "C59AFBC5-3D72-4292-A94E-79B893E9F9C4";

options.RequireHttpsMetadata = false;
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;

// Use the authorization code flow.
options.ResponseType = OpenIdConnectResponseType.Code;
options.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;

// Note: setting the Authority allows the OIDC client middleware to automatically
// retrieve the identity provider's configuration and spare you from setting
// the different endpoints URIs or the token validation parameters explicitly.
options.Authority = "https://localhost:44213/";

options.Scope.Add("email");
options.Scope.Add("roles");

options.SecurityTokenValidator = new JwtSecurityTokenHandler
{
// Disable the built-in JWT claims mapping feature.
InboundClaimTypeMap = new Dictionary<string, string>()
};

options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "role";
});

services.AddControllersWithViews();

services.AddHttpClient();
}

public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();

app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(options =>
{
options.MapControllers();
options.MapDefaultControllerRoute();
});
}
}
}
28 changes: 28 additions & 0 deletions samples/Rentor/Rentor.Client/Views/Shared/Home.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@model string

<div class="jumbotron">
@if (User?.Identity?.IsAuthenticated ?? false) {
<h1>Welcome, @User.Identity.Name</h1>

<p>
@foreach (var claim in Context.User.Claims) {
<div>@claim.Type: <b>@claim.Value</b></div>
}
</p>

if (!string.IsNullOrEmpty(Model)) {
<h3>Message received from the resource controller: @Model</h3>
}

<form action="~/" method="post">
<button class="btn btn-lg btn-warning" type="submit">Query the resource controller</button>
</form>

<a class="btn btn-lg btn-danger" href="/logout">Sign out</a>
}

else {
<h1>Welcome, anonymous</h1>
<a class="btn btn-lg btn-success" href="/login">Sign in</a>
}
</div>
30 changes: 30 additions & 0 deletions samples/Rentor/Rentor.Client/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="" />
<meta name="author" content="" />

<title>Rentor.Client (custom store sample)</title>

<link href="~/stylesheets/bootstrap.min.css" rel="stylesheet" />
<link href="~/stylesheets/jumbotron-narrow.css" rel="stylesheet" />
</head>

<body>
<div class="container">
<div class="header">
<h3 class="text-muted">Your application (Rentor.Client)</h3>
</div>

@RenderBody()

<div class="footer">
<p>&copy; Your company 2021</p>
</div>

</div>
</body>
</html>
3 changes: 3 additions & 0 deletions samples/Rentor/Rentor.Client/Views/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
Binary file not shown.
Loading