mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
MC # Adding login handling + User controller (WIP)
This commit is contained in:
parent
1c6b1a3cce
commit
8b01e1e10c
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -14,7 +14,7 @@ namespace MyCore.Controllers
|
|||||||
[Authorize(Roles = "Admin")]
|
[Authorize(Roles = "Admin")]
|
||||||
[Route("api/iot")]
|
[Route("api/iot")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class IOTController : Controller
|
public class IOTController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IoTDeviceService _ioTDeviceService;
|
private readonly IoTDeviceService _ioTDeviceService;
|
||||||
|
|
||||||
|
|||||||
@ -3,12 +3,14 @@ using System.Collections.Generic;
|
|||||||
using System.IdentityModel.Tokens.Jwt;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using MyCore.Models;
|
||||||
|
|
||||||
namespace MyCore.Controllers
|
namespace MyCore.Controllers
|
||||||
{
|
{
|
||||||
@ -20,10 +22,20 @@ namespace MyCore.Controllers
|
|||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public IActionResult Create(string username, string password)
|
public ActionResult<UserInfo> Create(string username, string password)
|
||||||
{
|
{
|
||||||
|
var test = GenerateSHA256String(password);
|
||||||
|
|
||||||
if (IsValidUserAndPasswordCombination(username, password))
|
if (IsValidUserAndPasswordCombination(username, password))
|
||||||
return new ObjectResult("{\"Token\":\""+GenerateToken(username)+"\"}");
|
{
|
||||||
|
UserInfo user = new UserInfo();
|
||||||
|
user.FirstName = "Thomas";
|
||||||
|
user.LastName = "Fransolet";
|
||||||
|
user.Token = GenerateToken(username).ToString();
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
//return new ObjectResult("{\"Token\":\""+GenerateToken(username)+"\"}");
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,5 +68,24 @@ namespace MyCore.Controllers
|
|||||||
if (username == "Thomas" && password == "MonsieurMagic") { return true; }
|
if (username == "Thomas" && password == "MonsieurMagic") { return true; }
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string GenerateSHA256String(string inputString)
|
||||||
|
{
|
||||||
|
SHA256 sha256 = SHA256Managed.Create();
|
||||||
|
byte[] bytes = Encoding.UTF8.GetBytes(inputString);
|
||||||
|
byte[] hash = sha256.ComputeHash(bytes);
|
||||||
|
return GetStringFromHash(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetStringFromHash(byte[] hash)
|
||||||
|
{
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (int i = 0; i < hash.Length; i++)
|
||||||
|
{
|
||||||
|
result.Append(hash[i].ToString("X2"));
|
||||||
|
}
|
||||||
|
return result.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
77
MyCore/Controllers/UserController.cs
Normal file
77
MyCore/Controllers/UserController.cs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using MQTTnet;
|
||||||
|
using MQTTnet.Client;
|
||||||
|
using MQTTnet.Server;
|
||||||
|
using MyCore.Models;
|
||||||
|
using MyCore.Services;
|
||||||
|
|
||||||
|
namespace MyCore.Controllers
|
||||||
|
{
|
||||||
|
[Authorize(Roles = "Admin")]
|
||||||
|
[Route("api/user")]
|
||||||
|
[ApiController]
|
||||||
|
public class UserController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly UserService _userService;
|
||||||
|
|
||||||
|
public UserController(UserService userService)
|
||||||
|
{
|
||||||
|
_userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET api/user
|
||||||
|
/// <summary>
|
||||||
|
/// Get a list of user
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult<IEnumerable<UserInfo>> Get()
|
||||||
|
{
|
||||||
|
//return new string[] { "value1", "value2" };
|
||||||
|
//return _userService.GetUsers();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// GET api/user/5
|
||||||
|
/// <summary>
|
||||||
|
/// Get a specific user
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">id user</param>
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public ActionResult<UserInfo> Get(string id)
|
||||||
|
{
|
||||||
|
UserInfo user = new UserInfo();
|
||||||
|
user.FirstName = "Thomas";
|
||||||
|
user.Id = "01";
|
||||||
|
return user;
|
||||||
|
//return _userService.GetUser(id);
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
// POST api/values
|
||||||
|
[HttpPost]
|
||||||
|
public void Post([FromBody] string value)
|
||||||
|
{
|
||||||
|
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT api/values/5
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
public void Put(int id, [FromBody] string value)
|
||||||
|
{
|
||||||
|
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE api/values/5
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public void Delete(int id)
|
||||||
|
{
|
||||||
|
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
41
MyCore/Models/UserInfo.cs
Normal file
41
MyCore/Models/UserInfo.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AspNetCore.Security.Jwt;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
|
||||||
|
namespace MyCore.Models
|
||||||
|
{
|
||||||
|
public class UserInfo : IAuthenticationUser
|
||||||
|
{
|
||||||
|
[BsonId]
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Role")]
|
||||||
|
public string Role { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Username")]
|
||||||
|
public string Username { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Password")]
|
||||||
|
public string Password { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("FirstName")]
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("LastName")]
|
||||||
|
public string LastName { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Token")]
|
||||||
|
public string Token { get; set; }
|
||||||
|
|
||||||
|
[BsonElement("Birthday")]
|
||||||
|
public string Birthday { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using AspNetCore.Security.Jwt;
|
|
||||||
|
|
||||||
|
|
||||||
namespace MyCore.Models
|
|
||||||
{
|
|
||||||
public class UserModel : IAuthenticationUser
|
|
||||||
{
|
|
||||||
public string Id { get; set; }
|
|
||||||
|
|
||||||
public string Password { get; set; }
|
|
||||||
|
|
||||||
public string Role { get; set; }
|
|
||||||
|
|
||||||
public DateTime DOB { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
54
MyCore/Services/UserService.cs
Normal file
54
MyCore/Services/UserService.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using MyCore.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace MyCore.Services
|
||||||
|
{
|
||||||
|
public class UserService
|
||||||
|
{
|
||||||
|
private readonly IMongoCollection<UserInfo> _Users;
|
||||||
|
|
||||||
|
public UserService(IConfiguration config)
|
||||||
|
{
|
||||||
|
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
|
||||||
|
var database = client.GetDatabase("MyCoreDb");
|
||||||
|
_Users = database.GetCollection<UserInfo>("Users");
|
||||||
|
}
|
||||||
|
public List<UserInfo> GetUsers()
|
||||||
|
{
|
||||||
|
return _Users.Find(m => true).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserInfo GetUser(string id)
|
||||||
|
{
|
||||||
|
return _Users.Find<UserInfo>(m => m.Id == id).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserInfo CreateUser(UserInfo user)
|
||||||
|
{
|
||||||
|
_Users.InsertOne(user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public void Update(string id, Book bookIn)
|
||||||
|
{
|
||||||
|
_books.ReplaceOne(book => book.Id == id, bookIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(Book bookIn)
|
||||||
|
{
|
||||||
|
_books.DeleteOne(book => book.Id == bookIn.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(string id)
|
||||||
|
{
|
||||||
|
_books.DeleteOne(book => book.Id == id);
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -85,7 +85,7 @@ namespace MyCore
|
|||||||
{
|
{
|
||||||
|
|
||||||
app.UseCors(
|
app.UseCors(
|
||||||
options => options.WithOrigins("http://localhost:4200").AllowAnyMethod()
|
options => options.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Enable middleware to serve generated Swagger as a JSON endpoint.
|
// Enable middleware to serve generated Swagger as a JSON endpoint.
|
||||||
|
|||||||
@ -29,6 +29,17 @@
|
|||||||
It's a mqtt publish test ! :)
|
It's a mqtt publish test ! :)
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.UserController.Get">
|
||||||
|
<summary>
|
||||||
|
Get a list of user
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.UserController.Get(System.String)">
|
||||||
|
<summary>
|
||||||
|
Get a specific user
|
||||||
|
</summary>
|
||||||
|
<param name="id">id user</param>
|
||||||
|
</member>
|
||||||
<member name="M:MyCore.Controllers.ValuesController.Get">
|
<member name="M:MyCore.Controllers.ValuesController.Get">
|
||||||
<summary>
|
<summary>
|
||||||
It's a test ! :)
|
It's a test ! :)
|
||||||
|
|||||||
@ -29,6 +29,17 @@
|
|||||||
It's a mqtt publish test ! :)
|
It's a mqtt publish test ! :)
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.UserController.Get">
|
||||||
|
<summary>
|
||||||
|
Get a list of user
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="M:MyCore.Controllers.UserController.Get(System.String)">
|
||||||
|
<summary>
|
||||||
|
Get a specific user
|
||||||
|
</summary>
|
||||||
|
<param name="id">id user</param>
|
||||||
|
</member>
|
||||||
<member name="M:MyCore.Controllers.ValuesController.Get">
|
<member name="M:MyCore.Controllers.ValuesController.Get">
|
||||||
<summary>
|
<summary>
|
||||||
It's a test ! :)
|
It's a test ! :)
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user