ajout controller Accounts api

This commit is contained in:
Julien LEICHER 2021-12-15 14:24:42 +01:00
parent 6519305560
commit c9b27393a0
No known key found for this signature in database
GPG Key ID: BE0761B6A007EB96
8 changed files with 119 additions and 3 deletions

22
.vscode/launch.json vendored
View File

@ -26,6 +26,28 @@
"/Views": "${workspaceFolder}/Views"
}
},
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (api)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Apps/HackerNet.Api/bin/Debug/net6.0/HackerNet.Api.dll",
"args": [],
"cwd": "${workspaceFolder}/Apps/HackerNet.Api",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",

12
.vscode/tasks.json vendored
View File

@ -13,6 +13,18 @@
],
"problemMatcher": "$msCompile"
},
{
"label": "buildapi",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Apps/HackerNet.Api/HackerNet.Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",

View File

@ -0,0 +1,39 @@
using HackerNet.Api.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace HackerNet.Api.Controllers;
[ApiController]
[Route("/api/accounts")]
public class AccountsController : ControllerBase
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
public AccountsController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
[HttpGet("me")]
public ActionResult Me()
{
throw new NotImplementedException();
}
[HttpPost]
public async Task<ActionResult> Signup(SignupLoginViewModel cmd)
{
var user = new IdentityUser(cmd.Username);
var result = await _userManager.CreateAsync(user, cmd.Password);
if (!result.Succeeded)
{
return BadRequest();
}
return CreatedAtAction(nameof(Me), null);
}
}

View File

@ -5,6 +5,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
<PackageReference Include="NSwag.AspNetCore" Version="13.15.3" />
</ItemGroup>

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace HackerNet.Api.Models;
public class SignupLoginViewModel
{
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}

View File

@ -1,9 +1,23 @@
using HackerNet.Infrastructure.AspNet;
using HackerNet.Infrastructure.Repositories.EntityFramework;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHackerNetServicesEntityFramework();
builder.Services.AddHackerNetServicesEntityFramework(builder.Configuration);
builder.Services.AddControllers();
builder.Services
.AddIdentityCore<IdentityUser>()
.AddRoles<IdentityRole>()
.AddSignInManager()
.AddEntityFrameworkStores<HackerContext>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer();
builder.Services.AddOpenApiDocument(d =>
{
d.Title = "HackerNet API";
@ -16,6 +30,9 @@ app.MigrateDatabase();
app.UseOpenApi();
app.UseSwaggerUi3();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/", () => "Hello World!");
app.MapControllers();

View File

@ -24,3 +24,12 @@ Content-Type: application/json
{
"content": "Contenu du commentaire"
}
###
POST {{url}}/api/accounts
Content-Type: application/json
{
}

View File

@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"Default": "Data Source=../HackerNet.Web/hackernet.db"
}
}