reintroduce microsoft login. upgrade a bunch of stuff.

This commit is contained in:
2023-01-19 16:20:41 -06:00
parent 2a423bcb93
commit 1d0beaf69f
29 changed files with 601 additions and 483 deletions

View File

@@ -38,23 +38,8 @@ public class UserController : ControllerBase
};
}
[HttpPut("GuestLogout")]
public async Task<IActionResult> GuestLogout()
{
var signoutTask = HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
var userId = User?.GetGuestUserId();
if (!string.IsNullOrEmpty(userId))
{
connectionManager.Unsubscribe(userId);
}
await signoutTask;
return Ok();
}
[HttpGet("Token")]
public ActionResult<CreateTokenResponse> GetToken()
public ActionResult<CreateTokenResponse> GetWebSocketToken()
{
var userId = User.GetShogiUserId();
var displayName = User.DisplayName();
@@ -83,4 +68,19 @@ public class UserController : ControllerBase
}
return Ok();
}
[HttpPut("GuestLogout")]
public async Task<IActionResult> GuestLogout()
{
var signOutTask = HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
var userId = User?.GetGuestUserId();
if (!string.IsNullOrEmpty(userId))
{
connectionManager.Unsubscribe(userId);
}
await signOutTask;
return Ok();
}
}

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.HttpLogging;
using Microsoft.Identity.Web;
using Microsoft.OpenApi.Models;
using Shogi.Api.Managers;
using Shogi.Api.Repositories;
@@ -119,9 +120,9 @@ namespace Shogi.Api
static void AddJwtAuth(WebApplicationBuilder builder)
{
//builder.Services
// .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
//.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
}
static void AddCookieAuth(WebApplicationBuilder builder)

View File

@@ -7,41 +7,46 @@ namespace Shogi.Api.Repositories;
public class UserRepository : IUserRepository
{
private readonly string connectionString;
private readonly string connectionString;
public UserRepository(IConfiguration configuration)
{
connectionString = configuration.GetConnectionString("ShogiDatabase");
}
public UserRepository(IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("ShogiDatabase");
if (string.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException("Connection string for database is empty.");
}
this.connectionString = connectionString;
}
public async Task CreateUser(User user)
{
using var connection = new SqlConnection(connectionString);
await connection.ExecuteAsync(
"user.CreateUser",
new
{
Name = user.Id,
DisplayName = user.DisplayName,
Platform = user.LoginPlatform.ToString()
},
commandType: CommandType.StoredProcedure);
}
public async Task CreateUser(User user)
{
using var connection = new SqlConnection(connectionString);
await connection.ExecuteAsync(
"user.CreateUser",
new
{
Name = user.Id,
DisplayName = user.DisplayName,
Platform = user.LoginPlatform.ToString()
},
commandType: CommandType.StoredProcedure);
}
public async Task<User?> ReadUser(string id)
{
using var connection = new SqlConnection(connectionString);
var results = await connection.QueryAsync<User>(
"user.ReadUser",
new { Name = id },
commandType: CommandType.StoredProcedure);
public async Task<User?> ReadUser(string id)
{
using var connection = new SqlConnection(connectionString);
var results = await connection.QueryAsync<User>(
"user.ReadUser",
new { Name = id },
commandType: CommandType.StoredProcedure);
return results.FirstOrDefault();
}
return results.FirstOrDefault();
}
}
public interface IUserRepository
{
Task CreateUser(User user);
Task<User?> ReadUser(string id);
Task CreateUser(User user);
Task<User?> ReadUser(string id);
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>5</AnalysisLevel>
<Nullable>enable</Nullable>
@@ -24,12 +24,13 @@
<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="Azure.Identity" Version="1.6.1" />
<PackageReference Include="Azure.Identity" Version="1.8.1" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="FluentValidation" Version="11.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.25.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
</ItemGroup>