Add project files.

This commit is contained in:
Olga Nemt 2025-01-15 17:09:56 +05:00
parent 201ac59c79
commit 0c45eb2529
17 changed files with 360 additions and 0 deletions

30
.dockerignore Normal file
View 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/**

View File

@ -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;
}
}

View 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>

View File

@ -0,0 +1,6 @@
@DD.Keycloak.Client.Web_HostAddress = http://localhost:5260
GET {{DD.Keycloak.Client.Web_HostAddress}}/weatherforecast/
Accept: application/json
###

View 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();

View 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
}
}
}

View 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; }
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

28
DD.Keycloak.Client.sln Normal file
View 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

View 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; }
}

View 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>

View File

@ -0,0 +1,5 @@
namespace DD.Keycloak.Client;
public interface IKeycloakClient
{
Task<string> GetToken();
}

View 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; }
}

View 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;
}
}

View 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>()!);
});
}
}

View 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"
}