ajout controller Accounts api
This commit is contained in:
parent
6519305560
commit
c9b27393a0
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -26,10 +26,32 @@
|
|||||||
"/Views": "${workspaceFolder}/Views"
|
"/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",
|
"name": ".NET Core Attach",
|
||||||
"type": "coreclr",
|
"type": "coreclr",
|
||||||
"request": "attach"
|
"request": "attach"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
14
.vscode/tasks.json
vendored
14
.vscode/tasks.json
vendored
@ -13,6 +13,18 @@
|
|||||||
],
|
],
|
||||||
"problemMatcher": "$msCompile"
|
"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",
|
"label": "publish",
|
||||||
"command": "dotnet",
|
"command": "dotnet",
|
||||||
@ -39,4 +51,4 @@
|
|||||||
"problemMatcher": "$msCompile"
|
"problemMatcher": "$msCompile"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
39
Apps/HackerNet.Api/Controllers/AccountsController.cs
Normal file
39
Apps/HackerNet.Api/Controllers/AccountsController.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,6 +5,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
|
||||||
<PackageReference Include="NSwag.AspNetCore" Version="13.15.3" />
|
<PackageReference Include="NSwag.AspNetCore" Version="13.15.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
13
Apps/HackerNet.Api/Models/SignupLoginViewModel.cs
Normal file
13
Apps/HackerNet.Api/Models/SignupLoginViewModel.cs
Normal 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; }
|
||||||
|
}
|
||||||
@ -1,9 +1,23 @@
|
|||||||
using HackerNet.Infrastructure.AspNet;
|
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);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
builder.Services.AddHackerNetServicesEntityFramework();
|
builder.Services.AddHackerNetServicesEntityFramework(builder.Configuration);
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
builder.Services
|
||||||
|
.AddIdentityCore<IdentityUser>()
|
||||||
|
.AddRoles<IdentityRole>()
|
||||||
|
.AddSignInManager()
|
||||||
|
.AddEntityFrameworkStores<HackerContext>();
|
||||||
|
|
||||||
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||||
|
.AddJwtBearer();
|
||||||
|
|
||||||
builder.Services.AddOpenApiDocument(d =>
|
builder.Services.AddOpenApiDocument(d =>
|
||||||
{
|
{
|
||||||
d.Title = "HackerNet API";
|
d.Title = "HackerNet API";
|
||||||
@ -16,6 +30,9 @@ app.MigrateDatabase();
|
|||||||
app.UseOpenApi();
|
app.UseOpenApi();
|
||||||
app.UseSwaggerUi3();
|
app.UseSwaggerUi3();
|
||||||
|
|
||||||
|
app.UseAuthentication();
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapGet("/", () => "Hello World!");
|
app.MapGet("/", () => "Hello World!");
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
|
|||||||
@ -24,3 +24,12 @@ Content-Type: application/json
|
|||||||
{
|
{
|
||||||
"content": "Contenu du commentaire"
|
"content": "Contenu du commentaire"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
POST {{url}}/api/accounts
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@ -4,5 +4,8 @@
|
|||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"Default": "Data Source=../HackerNet.Web/hackernet.db"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user