Add project files.
This commit is contained in:
parent
201ac59c79
commit
0c45eb2529
30
.dockerignore
Normal file
30
.dockerignore
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
**/.classpath
|
||||||
|
**/.dockerignore
|
||||||
|
**/.env
|
||||||
|
**/.git
|
||||||
|
**/.gitignore
|
||||||
|
**/.project
|
||||||
|
**/.settings
|
||||||
|
**/.toolstarget
|
||||||
|
**/.vs
|
||||||
|
**/.vscode
|
||||||
|
**/*.*proj.user
|
||||||
|
**/*.dbmdl
|
||||||
|
**/*.jfm
|
||||||
|
**/azds.yaml
|
||||||
|
**/bin
|
||||||
|
**/charts
|
||||||
|
**/docker-compose*
|
||||||
|
**/Dockerfile*
|
||||||
|
**/node_modules
|
||||||
|
**/npm-debug.log
|
||||||
|
**/obj
|
||||||
|
**/secrets.dev.yaml
|
||||||
|
**/values.dev.yaml
|
||||||
|
LICENSE
|
||||||
|
README.md
|
||||||
|
!**/.gitignore
|
||||||
|
!.git/HEAD
|
||||||
|
!.git/config
|
||||||
|
!.git/packed-refs
|
||||||
|
!.git/refs/heads/**
|
@ -0,0 +1,28 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace DD.Keycloak.Client.Web.Controllers;
|
||||||
|
[ApiController]
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class WeatherForecastController : ControllerBase
|
||||||
|
{
|
||||||
|
private static readonly string[] Summaries = new[]
|
||||||
|
{
|
||||||
|
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly ILogger<WeatherForecastController> _logger;
|
||||||
|
private readonly IKeycloakClient _test;
|
||||||
|
|
||||||
|
public WeatherForecastController(ILogger<WeatherForecastController> logger, IKeycloakClient test)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_test = test;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<string> Get()
|
||||||
|
{
|
||||||
|
var token = await _test.GetToken();
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
13
DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.csproj
Normal file
13
DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.csproj
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\DD.Keycloak.Client\DD.Keycloak.Client.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
6
DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.http
Normal file
6
DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.http
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
@DD.Keycloak.Client.Web_HostAddress = http://localhost:5260
|
||||||
|
|
||||||
|
GET {{DD.Keycloak.Client.Web_HostAddress}}/weatherforecast/
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
23
DD.Keycloak.Client.Web/Program.cs
Normal file
23
DD.Keycloak.Client.Web/Program.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using DD.Keycloak.Client;
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
builder.Services.AddTest();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
app.Run();
|
36
DD.Keycloak.Client.Web/Properties/launchSettings.json
Normal file
36
DD.Keycloak.Client.Web/Properties/launchSettings.json
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"applicationUrl": "http://localhost:5260"
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"applicationUrl": "https://localhost:7117;http://localhost:5260"
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:59674/",
|
||||||
|
"sslPort": 44347
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
DD.Keycloak.Client.Web/WeatherForecast.cs
Normal file
12
DD.Keycloak.Client.Web/WeatherForecast.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
namespace DD.Keycloak.Client.Web;
|
||||||
|
|
||||||
|
public class WeatherForecast
|
||||||
|
{
|
||||||
|
public DateOnly Date { get; set; }
|
||||||
|
|
||||||
|
public int TemperatureC { get; set; }
|
||||||
|
|
||||||
|
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||||
|
|
||||||
|
public string? Summary { get; set; }
|
||||||
|
}
|
8
DD.Keycloak.Client.Web/appsettings.Development.json
Normal file
8
DD.Keycloak.Client.Web/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
DD.Keycloak.Client.Web/appsettings.json
Normal file
9
DD.Keycloak.Client.Web/appsettings.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
28
DD.Keycloak.Client.sln
Normal file
28
DD.Keycloak.Client.sln
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.12.35527.113 d17.12
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DD.Keycloak.Client", "DD.Keycloak.Client\DD.Keycloak.Client.csproj", "{DC1BF1A9-754F-4E84-BBE6-DD01DC908B33}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DD.Keycloak.Client.Web", "DD.Keycloak.Client.Web\DD.Keycloak.Client.Web.csproj", "{123E88D0-5102-4A39-B9A8-74542E8F773D}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{DC1BF1A9-754F-4E84-BBE6-DD01DC908B33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{DC1BF1A9-754F-4E84-BBE6-DD01DC908B33}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{DC1BF1A9-754F-4E84-BBE6-DD01DC908B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{DC1BF1A9-754F-4E84-BBE6-DD01DC908B33}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{123E88D0-5102-4A39-B9A8-74542E8F773D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{123E88D0-5102-4A39-B9A8-74542E8F773D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{123E88D0-5102-4A39-B9A8-74542E8F773D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{123E88D0-5102-4A39-B9A8-74542E8F773D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
36
DD.Keycloak.Client/AuthUser.cs
Normal file
36
DD.Keycloak.Client/AuthUser.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DD.Keycloak.Client;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Настройки credentials для авторизации
|
||||||
|
/// </summary>
|
||||||
|
public class AuthUser
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
[DisplayName("adasd")]
|
||||||
|
public required string Username { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
[DisplayName("adasd2")]
|
||||||
|
public required string Password { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public required string ClientId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public required string GrantType { get; set; }
|
||||||
|
}
|
23
DD.Keycloak.Client/DD.Keycloak.Client.csproj
Normal file
23
DD.Keycloak.Client/DD.Keycloak.Client.csproj
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
|
||||||
|
<PackageReference Include="RestSharp" Version="112.1.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
5
DD.Keycloak.Client/IKeycloakClient.cs
Normal file
5
DD.Keycloak.Client/IKeycloakClient.cs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
namespace DD.Keycloak.Client;
|
||||||
|
public interface IKeycloakClient
|
||||||
|
{
|
||||||
|
Task<string> GetToken();
|
||||||
|
}
|
16
DD.Keycloak.Client/JwtToken.cs
Normal file
16
DD.Keycloak.Client/JwtToken.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DD.Keycloak.Client;
|
||||||
|
public class JwtToken
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
[JsonPropertyName("access_token")]
|
||||||
|
public required string AccessToken { get; set; }
|
||||||
|
}
|
55
DD.Keycloak.Client/KeycloakClient.cs
Normal file
55
DD.Keycloak.Client/KeycloakClient.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using RestSharp;
|
||||||
|
|
||||||
|
namespace DD.Keycloak.Client;
|
||||||
|
public class KeycloakClient : IKeycloakClient
|
||||||
|
{
|
||||||
|
private AuthUser authUser;
|
||||||
|
|
||||||
|
private IRestClient client;
|
||||||
|
|
||||||
|
private string keycloakBaseUrl;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public KeycloakClient(IRestClient client)
|
||||||
|
{
|
||||||
|
var builder = new ConfigurationBuilder()
|
||||||
|
//.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
|
.SetBasePath("C:\\Users\\admin\\source\\repos\\DD.Keycloak.Client\\DD.KeyCloak.Client\\bin\\Debug\\net9.0")
|
||||||
|
.AddJsonFile("appsettings.json");
|
||||||
|
|
||||||
|
var configuration = builder.Build();
|
||||||
|
|
||||||
|
this.authUser = configuration.GetSection("AuthUser").Get<AuthUser>()!;
|
||||||
|
this.client = client;
|
||||||
|
//new RestClient();
|
||||||
|
|
||||||
|
var keycloackHost = configuration.GetSection("KeycloackHost").Value!;
|
||||||
|
this.keycloakBaseUrl = $"http://{keycloackHost}/realms/Persistence/protocol/openid-connect/token";
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetToken()
|
||||||
|
{
|
||||||
|
var request = new RestRequest(keycloakBaseUrl, Method.Post);
|
||||||
|
request.AddParameter("username", authUser.Username);
|
||||||
|
request.AddParameter("password", authUser.Password);
|
||||||
|
request.AddParameter("client_id", authUser.ClientId);
|
||||||
|
request.AddParameter("grant_type", authUser.GrantType);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var content = await client.PostAsync<JwtToken>(request);
|
||||||
|
if (content != null)
|
||||||
|
{
|
||||||
|
return content.AccessToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
;
|
||||||
|
}
|
||||||
|
return String.Empty;
|
||||||
|
}
|
||||||
|
}
|
22
DD.Keycloak.Client/ServiceCollectionExtensions.cs
Normal file
22
DD.Keycloak.Client/ServiceCollectionExtensions.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using RestSharp;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DD.Keycloak.Client;
|
||||||
|
public static class ServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static void AddTest(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
|
||||||
|
services.AddTransient<IRestClient, RestClient>();
|
||||||
|
services.AddSingleton<IKeycloakClient>((provider) =>
|
||||||
|
{
|
||||||
|
return new KeycloakClient(provider.GetService<IRestClient>()!);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
10
DD.Keycloak.Client/appsettings.json
Normal file
10
DD.Keycloak.Client/appsettings.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"AuthUser": {
|
||||||
|
"username": "myuser",
|
||||||
|
"password": 12345,
|
||||||
|
"clientId": "webapi",
|
||||||
|
"grantType": "password",
|
||||||
|
"http://schemas.xmlsoap.org/ws/2005/05/identity /claims/nameidentifier": "7d9f3574-6574-4ca3-845a-0276eb4aa8f6"
|
||||||
|
},
|
||||||
|
"KeycloackHost": "192.168.0.10:8321"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user