From 0c45eb2529f3dadb0c32b8cdf7dccaa07f94d644 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Wed, 15 Jan 2025 17:09:56 +0500 Subject: [PATCH] Add project files. --- .dockerignore | 30 ++++++++++ .../Controllers/WeatherForecastController.cs | 28 ++++++++++ .../DD.Keycloak.Client.Web.csproj | 13 +++++ .../DD.Keycloak.Client.Web.http | 6 ++ DD.Keycloak.Client.Web/Program.cs | 23 ++++++++ .../Properties/launchSettings.json | 36 ++++++++++++ DD.Keycloak.Client.Web/WeatherForecast.cs | 12 ++++ .../appsettings.Development.json | 8 +++ DD.Keycloak.Client.Web/appsettings.json | 9 +++ DD.Keycloak.Client.sln | 28 ++++++++++ DD.Keycloak.Client/AuthUser.cs | 36 ++++++++++++ DD.Keycloak.Client/DD.Keycloak.Client.csproj | 23 ++++++++ DD.Keycloak.Client/IKeycloakClient.cs | 5 ++ DD.Keycloak.Client/JwtToken.cs | 16 ++++++ DD.Keycloak.Client/KeycloakClient.cs | 55 +++++++++++++++++++ .../ServiceCollectionExtensions.cs | 22 ++++++++ DD.Keycloak.Client/appsettings.json | 10 ++++ 17 files changed, 360 insertions(+) create mode 100644 .dockerignore create mode 100644 DD.Keycloak.Client.Web/Controllers/WeatherForecastController.cs create mode 100644 DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.csproj create mode 100644 DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.http create mode 100644 DD.Keycloak.Client.Web/Program.cs create mode 100644 DD.Keycloak.Client.Web/Properties/launchSettings.json create mode 100644 DD.Keycloak.Client.Web/WeatherForecast.cs create mode 100644 DD.Keycloak.Client.Web/appsettings.Development.json create mode 100644 DD.Keycloak.Client.Web/appsettings.json create mode 100644 DD.Keycloak.Client.sln create mode 100644 DD.Keycloak.Client/AuthUser.cs create mode 100644 DD.Keycloak.Client/DD.Keycloak.Client.csproj create mode 100644 DD.Keycloak.Client/IKeycloakClient.cs create mode 100644 DD.Keycloak.Client/JwtToken.cs create mode 100644 DD.Keycloak.Client/KeycloakClient.cs create mode 100644 DD.Keycloak.Client/ServiceCollectionExtensions.cs create mode 100644 DD.Keycloak.Client/appsettings.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/.dockerignore @@ -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/** \ No newline at end of file diff --git a/DD.Keycloak.Client.Web/Controllers/WeatherForecastController.cs b/DD.Keycloak.Client.Web/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..dd76876 --- /dev/null +++ b/DD.Keycloak.Client.Web/Controllers/WeatherForecastController.cs @@ -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 _logger; + private readonly IKeycloakClient _test; + + public WeatherForecastController(ILogger logger, IKeycloakClient test) + { + _logger = logger; + _test = test; + } + + [HttpGet] + public async Task Get() + { + var token = await _test.GetToken(); + return token; + } +} diff --git a/DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.csproj b/DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.csproj new file mode 100644 index 0000000..9f9948e --- /dev/null +++ b/DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.http b/DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.http new file mode 100644 index 0000000..e433208 --- /dev/null +++ b/DD.Keycloak.Client.Web/DD.Keycloak.Client.Web.http @@ -0,0 +1,6 @@ +@DD.Keycloak.Client.Web_HostAddress = http://localhost:5260 + +GET {{DD.Keycloak.Client.Web_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/DD.Keycloak.Client.Web/Program.cs b/DD.Keycloak.Client.Web/Program.cs new file mode 100644 index 0000000..6a5f02c --- /dev/null +++ b/DD.Keycloak.Client.Web/Program.cs @@ -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(); diff --git a/DD.Keycloak.Client.Web/Properties/launchSettings.json b/DD.Keycloak.Client.Web/Properties/launchSettings.json new file mode 100644 index 0000000..dc21633 --- /dev/null +++ b/DD.Keycloak.Client.Web/Properties/launchSettings.json @@ -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 + } + } +} \ No newline at end of file diff --git a/DD.Keycloak.Client.Web/WeatherForecast.cs b/DD.Keycloak.Client.Web/WeatherForecast.cs new file mode 100644 index 0000000..f1eeedd --- /dev/null +++ b/DD.Keycloak.Client.Web/WeatherForecast.cs @@ -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; } +} diff --git a/DD.Keycloak.Client.Web/appsettings.Development.json b/DD.Keycloak.Client.Web/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/DD.Keycloak.Client.Web/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/DD.Keycloak.Client.Web/appsettings.json b/DD.Keycloak.Client.Web/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/DD.Keycloak.Client.Web/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/DD.Keycloak.Client.sln b/DD.Keycloak.Client.sln new file mode 100644 index 0000000..e47e1d4 --- /dev/null +++ b/DD.Keycloak.Client.sln @@ -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 diff --git a/DD.Keycloak.Client/AuthUser.cs b/DD.Keycloak.Client/AuthUser.cs new file mode 100644 index 0000000..bc00f24 --- /dev/null +++ b/DD.Keycloak.Client/AuthUser.cs @@ -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; + +/// +/// Настройки credentials для авторизации +/// +public class AuthUser +{ + /// + /// + /// + [DisplayName("adasd")] + public required string Username { get; set; } + + /// + /// + /// + [DisplayName("adasd2")] + public required string Password { get; set; } + + /// + /// + /// + public required string ClientId { get; set; } + + /// + /// + /// + public required string GrantType { get; set; } +} diff --git a/DD.Keycloak.Client/DD.Keycloak.Client.csproj b/DD.Keycloak.Client/DD.Keycloak.Client.csproj new file mode 100644 index 0000000..542c28b --- /dev/null +++ b/DD.Keycloak.Client/DD.Keycloak.Client.csproj @@ -0,0 +1,23 @@ + + + + net9.0 + enable + enable + + + + + + + + + + + + + Always + + + + diff --git a/DD.Keycloak.Client/IKeycloakClient.cs b/DD.Keycloak.Client/IKeycloakClient.cs new file mode 100644 index 0000000..1b7d2b2 --- /dev/null +++ b/DD.Keycloak.Client/IKeycloakClient.cs @@ -0,0 +1,5 @@ +namespace DD.Keycloak.Client; +public interface IKeycloakClient +{ + Task GetToken(); +} diff --git a/DD.Keycloak.Client/JwtToken.cs b/DD.Keycloak.Client/JwtToken.cs new file mode 100644 index 0000000..c8e3f13 --- /dev/null +++ b/DD.Keycloak.Client/JwtToken.cs @@ -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 +{ + /// + /// + /// + [JsonPropertyName("access_token")] + public required string AccessToken { get; set; } +} diff --git a/DD.Keycloak.Client/KeycloakClient.cs b/DD.Keycloak.Client/KeycloakClient.cs new file mode 100644 index 0000000..efbd0b9 --- /dev/null +++ b/DD.Keycloak.Client/KeycloakClient.cs @@ -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; + + /// + /// + /// + 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()!; + this.client = client; + //new RestClient(); + + var keycloackHost = configuration.GetSection("KeycloackHost").Value!; + this.keycloakBaseUrl = $"http://{keycloackHost}/realms/Persistence/protocol/openid-connect/token"; + } + + public async Task 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(request); + if (content != null) + { + return content.AccessToken; + } + } + catch (Exception ex) + { + ; + } + return String.Empty; + } +} diff --git a/DD.Keycloak.Client/ServiceCollectionExtensions.cs b/DD.Keycloak.Client/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..a448143 --- /dev/null +++ b/DD.Keycloak.Client/ServiceCollectionExtensions.cs @@ -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(); + services.AddSingleton((provider) => + { + return new KeycloakClient(provider.GetService()!); + }); + } +} diff --git a/DD.Keycloak.Client/appsettings.json b/DD.Keycloak.Client/appsettings.json new file mode 100644 index 0000000..3d3a7fb --- /dev/null +++ b/DD.Keycloak.Client/appsettings.json @@ -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" +} \ No newline at end of file