Beta 1.0.0.2
Add
1. UserController - > CreateUser
-> AlterUser
2. JWTAuthContoller - > Login
-> RefreshToken
This commit is contained in:
@@ -20,6 +20,88 @@ namespace APISocietyToolBox.Controllers
|
|||||||
_Context = context;
|
_Context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("CreateUser")]
|
||||||
|
public async Task<ActionResult<string>> CreateUser(UserRequestModel UserRequest)
|
||||||
|
{
|
||||||
|
CreatePasswordHash(UserRequest.LoginPassword, out byte[] passwordHash, out byte[] passwordSalt);
|
||||||
|
UserModel User = new UserModel();
|
||||||
|
User.UserID = UserRequest.UserID;
|
||||||
|
User.UserName = UserRequest.UserName;
|
||||||
|
User.UserRole = UserRequest.UserRole;
|
||||||
|
User.UserRoleID = UserRequest.UserRoleID;
|
||||||
|
User.LoginName = UserRequest.LoginName;
|
||||||
|
User.LoginPassword = BitConverter.ToString(passwordHash).Replace("-", "");
|
||||||
|
User.PasswordSalt = BitConverter.ToString(passwordSalt).Replace("-", "");// System.Text.Encoding.UTF8.GetString(passwordSalt);
|
||||||
|
User.MemberID = UserRequest.MemberID;
|
||||||
|
User.PropertyID = UserRequest.PropertyID;
|
||||||
|
string pStrJSONUser = JsonConvert.SerializeObject(User);
|
||||||
|
//pStrJSONUser = Regex.Replace(pStrJSONUser, @"[^\x20-\x7E]", "");
|
||||||
|
string pStrUserXML = ConvertFunctions.ConvertJsonToXmlR2(pStrJSONUser, "USER", "USERDETAILS");
|
||||||
|
//UserResponseModel UserResponse = new UserResponseModel();
|
||||||
|
var pQueryResponse = await _Context.UserCRUD.FromSqlRaw("EXEC sp_CreateUser @XMLUserData",
|
||||||
|
new SqlParameter("@XMLUserData", System.Data.SqlDbType.Xml) { Value = pStrUserXML })
|
||||||
|
.ToListAsync();
|
||||||
|
//var oUserResponse = JsonConvert.DeserializeObject<UserResponseModel>(pQueryResponse?.ToString());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (pQueryResponse[0].Status == 200)
|
||||||
|
{
|
||||||
|
return Ok(pQueryResponse);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest(pQueryResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("AlterUser")]
|
||||||
|
public async Task<ActionResult<string>> AlterUser(UserRequestModel UserRequest)
|
||||||
|
{
|
||||||
|
CreatePasswordHash(UserRequest.LoginPassword, out byte[] passwordHash, out byte[] passwordSalt);
|
||||||
|
UserModel User = new UserModel();
|
||||||
|
User.UserID = UserRequest.UserID;
|
||||||
|
User.UserName = UserRequest.UserName;
|
||||||
|
User.UserRole = UserRequest.UserRole;
|
||||||
|
User.UserRoleID = UserRequest.UserRoleID;
|
||||||
|
User.LoginName = UserRequest.LoginName;
|
||||||
|
User.LoginPassword = BitConverter.ToString(passwordHash).Replace("-", "");
|
||||||
|
User.PasswordSalt = BitConverter.ToString(passwordSalt).Replace("-", "");// System.Text.Encoding.UTF8.GetString(passwordSalt);
|
||||||
|
|
||||||
|
User.PropertyID = UserRequest.PropertyID;
|
||||||
|
string pStrJSONUser = JsonConvert.SerializeObject(User);
|
||||||
|
//pStrJSONUser = Regex.Replace(pStrJSONUser, @"[^\x20-\x7E]", "");
|
||||||
|
string pStrUserXML = ConvertFunctions.ConvertJsonToXmlR2(pStrJSONUser, "USER", "USERDETAILS");
|
||||||
|
//UserResponseModel UserResponse = new UserResponseModel();
|
||||||
|
var pQueryResponse = await _Context.UserCRUD.FromSqlRaw("EXEC sp_AlterUser @XMLUserData",
|
||||||
|
new SqlParameter("@XMLUserData", System.Data.SqlDbType.Xml) { Value = pStrUserXML })
|
||||||
|
.ToListAsync();
|
||||||
|
//var oUserResponse = JsonConvert.DeserializeObject<UserResponseModel>(pQueryResponse?.ToString());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (pQueryResponse[0].Status == 200)
|
||||||
|
{
|
||||||
|
return Ok(pQueryResponse);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest(pQueryResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
|
||||||
|
{
|
||||||
|
using (var hmac = new HMACSHA512())
|
||||||
|
{
|
||||||
|
passwordSalt = hmac.Key;
|
||||||
|
passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ namespace APISocietyToolBox.Data
|
|||||||
public class DataContext : DbContext
|
public class DataContext : DbContext
|
||||||
{
|
{
|
||||||
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
|
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
|
||||||
public DbSet<UserResponseModel> UserCreate { get; set; }
|
public DbSet<UserResponseModel> UserCRUD { get; set; }
|
||||||
public DbSet<LoginResponseModel> LoginUserValidate { get; set; }
|
public DbSet<LoginResponseModel> LoginUserValidate { get; set; }
|
||||||
public DbSet<UserTokenModel> getUserToken { get; set; }
|
public DbSet<UserTokenModel> getUserToken { get; set; }
|
||||||
public DbSet<UserTokens> UserTokens { get; set; }
|
public DbSet<UserTokens> UserTokens { get; set; }
|
||||||
|
|||||||
@@ -0,0 +1,208 @@
|
|||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
|
||||||
|
namespace APISocietyToolBox.Functions
|
||||||
|
{
|
||||||
|
public class ConvertFunctions
|
||||||
|
{
|
||||||
|
public static string ConvertJsonToXml(string jsonString)
|
||||||
|
{
|
||||||
|
// Parse JSON
|
||||||
|
var jsonDocument = JsonDocument.Parse(jsonString);
|
||||||
|
|
||||||
|
// Create an XML document
|
||||||
|
var xmlDocument = new XDocument(
|
||||||
|
new XElement("Root",
|
||||||
|
ConvertJsonElementToXml(jsonDocument.RootElement)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return xmlDocument.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static XElement ConvertJsonElementToXml(JsonElement jsonElement)
|
||||||
|
{
|
||||||
|
var element = new XElement(jsonElement.ValueKind.ToString());
|
||||||
|
|
||||||
|
switch (jsonElement.ValueKind)
|
||||||
|
{
|
||||||
|
case JsonValueKind.Object:
|
||||||
|
foreach (var property in jsonElement.EnumerateObject())
|
||||||
|
{
|
||||||
|
element.Add(new XElement(property.Name, ConvertJsonElementToXml(property.Value)));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JsonValueKind.Array:
|
||||||
|
foreach (var item in jsonElement.EnumerateArray())
|
||||||
|
{
|
||||||
|
element.Add(ConvertJsonElementToXml(item));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JsonValueKind.String:
|
||||||
|
case JsonValueKind.Number:
|
||||||
|
case JsonValueKind.True:
|
||||||
|
case JsonValueKind.False:
|
||||||
|
element.Value = jsonElement.GetRawText();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case JsonValueKind.Null:
|
||||||
|
// Handle null values if needed
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
public static string ConvertJsonToXmlR2(string jsonString, string rootElementName, string objectElementName)
|
||||||
|
{
|
||||||
|
// Parse JSON to JObject
|
||||||
|
JObject jsonObject = JObject.Parse(jsonString);
|
||||||
|
|
||||||
|
// Create the root XML element with the user-defined name
|
||||||
|
XElement rootElement = new XElement(rootElementName);
|
||||||
|
|
||||||
|
// Create the object XML element with the user-defined name
|
||||||
|
XElement objectElement = new XElement(objectElementName);
|
||||||
|
|
||||||
|
// Convert JSON properties to XML elements with values
|
||||||
|
foreach (var property in jsonObject.Properties())
|
||||||
|
{
|
||||||
|
// Create XML element and add the value
|
||||||
|
XElement element = new XElement(property.Name, (property.Value.ToString().Replace("/", "/")));
|
||||||
|
|
||||||
|
// Add the element to the object element
|
||||||
|
objectElement.Add(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the object element to the root element
|
||||||
|
rootElement.Add(objectElement);
|
||||||
|
|
||||||
|
// Convert the XElement to an XML string
|
||||||
|
string xmlString = rootElement.ToString();
|
||||||
|
|
||||||
|
return xmlString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ConvertJsonToXmlR3(string jsonString, string rootElementName, string objectElementName)
|
||||||
|
{
|
||||||
|
// Parse JSON to JObject
|
||||||
|
JObject jsonObject = JObject.Parse(jsonString);
|
||||||
|
|
||||||
|
// Create the root XML element with the user-defined name
|
||||||
|
XElement rootElement = new XElement(rootElementName);
|
||||||
|
|
||||||
|
// Convert JSON properties to XML elements with values
|
||||||
|
foreach (var property in jsonObject.Properties())
|
||||||
|
{
|
||||||
|
if (property.Name == "TransactionDate" && property.Value.Type == JTokenType.Date)
|
||||||
|
{
|
||||||
|
// Special handling for TransactionDate property with date type
|
||||||
|
XElement dateElement = new XElement(SanitizeElementName(property.Name), ((DateTime)property.Value).ToString("yyyy-MM-ddTHH:mm:ss"));
|
||||||
|
rootElement.Add(dateElement);
|
||||||
|
}
|
||||||
|
else if (property.Name == "itemDetails" || property.Name == "ledgerDetails" || property.Name == "batchDetails" || property.Name == "billDetails" || property.Name == "otherDetails")
|
||||||
|
{
|
||||||
|
// Handle arrays separately
|
||||||
|
XElement arrayElement = new XElement(SanitizeElementName(property.Name + "Models"));
|
||||||
|
rootElement.Add(arrayElement);
|
||||||
|
|
||||||
|
foreach (var arrayItem in property.Value)
|
||||||
|
{
|
||||||
|
XElement objectElement = new XElement(objectElementName);
|
||||||
|
ConvertJTokenToXml(arrayItem, objectElement);
|
||||||
|
arrayElement.Add(objectElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (property.Value.Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
// If the property value is an array, recursively convert it to XML
|
||||||
|
rootElement.Add(ConvertArrayToXml(property, objectElementName));
|
||||||
|
}
|
||||||
|
else if (property.Value.Type == JTokenType.Object)
|
||||||
|
{
|
||||||
|
// If the property value is an object, recursively convert it to XML
|
||||||
|
XElement objectElement = new XElement(objectElementName);
|
||||||
|
ConvertJTokenToXml(property.Value, objectElement);
|
||||||
|
rootElement.Add(objectElement);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Create XML element and add the value
|
||||||
|
XElement element = new XElement(SanitizeElementName(property.Name), property.Value.ToString());
|
||||||
|
rootElement.Add(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the XElement to an XML string
|
||||||
|
string xmlString = rootElement.ToString();
|
||||||
|
|
||||||
|
return xmlString;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Helper method to recursively convert JToken to XML
|
||||||
|
private static XElement ConvertJTokenToXml(JToken token, XElement parentElement)
|
||||||
|
{
|
||||||
|
if (token.Type == JTokenType.Object)
|
||||||
|
{
|
||||||
|
foreach (var property in token.Children<JProperty>())
|
||||||
|
{
|
||||||
|
XElement childElement = new XElement(SanitizeElementName(property.Name));
|
||||||
|
parentElement.Add(childElement);
|
||||||
|
ConvertJTokenToXml(property.Value, childElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (token.Type == JTokenType.Array)
|
||||||
|
{
|
||||||
|
foreach (var arrayItem in token)
|
||||||
|
{
|
||||||
|
XElement objectElement = new XElement(parentElement.Name);
|
||||||
|
ConvertJTokenToXml(arrayItem, objectElement);
|
||||||
|
parentElement.Add(objectElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parentElement.Value = token.ToString();
|
||||||
|
}
|
||||||
|
return parentElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to convert JToken array to XML
|
||||||
|
private static XElement ConvertArrayToXml(JProperty property, string objectElementName)
|
||||||
|
{
|
||||||
|
XElement arrayElement = new XElement(SanitizeElementName(property.Name));
|
||||||
|
foreach (var arrayItem in property.Value)
|
||||||
|
{
|
||||||
|
XElement objectElement = new XElement(objectElementName);
|
||||||
|
ConvertJTokenToXml(arrayItem, objectElement);
|
||||||
|
arrayElement.Add(objectElement);
|
||||||
|
}
|
||||||
|
return arrayElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to sanitize XML element names
|
||||||
|
private static string SanitizeElementName(string name)
|
||||||
|
{
|
||||||
|
// Check if the name is not null or empty before replacing characters
|
||||||
|
if (!string.IsNullOrEmpty(name))
|
||||||
|
{
|
||||||
|
// Replace invalid characters with underscore
|
||||||
|
return name.Replace(" ", "_"); // You can adjust this sanitization logic as needed
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If the name is null or empty, return a default name or handle it according to your requirements
|
||||||
|
return "UnknownElement";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
-15
@@ -8,14 +8,16 @@ namespace APISocietyToolBox.Models
|
|||||||
{
|
{
|
||||||
public class UserModel
|
public class UserModel
|
||||||
{
|
{
|
||||||
public Int64? UserID { get; set; } = null;
|
[Key]
|
||||||
|
public Int32? UserID { get; set; } = null;
|
||||||
public string UserName { get; set; } = string.Empty;
|
public string UserName { get; set; } = string.Empty;
|
||||||
public string LoginName { get; set; } = string.Empty;
|
public string LoginName { get; set; } = string.Empty;
|
||||||
public string LoginPassword { get; set; } = string.Empty;
|
public string LoginPassword { get; set; } = string.Empty;
|
||||||
public string PasswordSalt { get; set; } = string.Empty;
|
public string PasswordSalt { get; set; } = string.Empty;
|
||||||
public string UserRole { get; set; } = string.Empty;
|
public string UserRole { get; set; } = string.Empty;
|
||||||
public Int64? UserRoleID { get; set; }
|
public Int32? UserRoleID { get; set; }
|
||||||
public Int64? PropertyID { get; set; }
|
public Int32? PropertyID { get; set; }
|
||||||
|
public int MemberID { get; set; }
|
||||||
public string? RefreshToken { get; set; } = string.Empty;
|
public string? RefreshToken { get; set; } = string.Empty;
|
||||||
public DateTime? TokenCreated { get; set; }
|
public DateTime? TokenCreated { get; set; }
|
||||||
public DateTime? TokenExpires { get; set; }
|
public DateTime? TokenExpires { get; set; }
|
||||||
@@ -37,9 +39,10 @@ namespace APISocietyToolBox.Models
|
|||||||
public string StatusMessage { get; set; } = string.Empty;
|
public string StatusMessage { get; set; } = string.Empty;
|
||||||
public string? UserName { get; set; } = string.Empty;
|
public string? UserName { get; set; } = string.Empty;
|
||||||
public string? UserRole { get; set; } = string.Empty;
|
public string? UserRole { get; set; } = string.Empty;
|
||||||
public Int64? UserRoleID { get; set; } = null;
|
public Int32? UserRoleID { get; set; } = null;
|
||||||
public Int64? UserID { get; set; } = null;
|
public Int32? UserID { get; set; } = null;
|
||||||
public int PropertyID { get; set; }
|
public int PropertyID { get; set; }
|
||||||
|
public int MemberID { get; set; }
|
||||||
public string? RefreshToken { get; set; } = string.Empty;
|
public string? RefreshToken { get; set; } = string.Empty;
|
||||||
public DateTime? TokenCreated { get; set; }
|
public DateTime? TokenCreated { get; set; }
|
||||||
public DateTime? TokenExpires { get; set; }
|
public DateTime? TokenExpires { get; set; }
|
||||||
@@ -51,14 +54,15 @@ namespace APISocietyToolBox.Models
|
|||||||
public int? Status { get; set; }
|
public int? Status { get; set; }
|
||||||
public string StatusMessage { get; set; } = string.Empty;
|
public string StatusMessage { get; set; } = string.Empty;
|
||||||
[Key]
|
[Key]
|
||||||
public Int64 UserID { get; set; }
|
public Int32 UserID { get; set; }
|
||||||
public Int64 UserRoleID { get; set; }
|
public Int32 UserRoleID { get; set; }
|
||||||
public string UserRole { get; set; } = string.Empty;
|
public string UserRole { get; set; } = string.Empty;
|
||||||
public string UserName { get; set; } = string.Empty;
|
public string UserName { get; set; } = string.Empty;
|
||||||
public string LoginPassword { get; set; } = string.Empty;
|
public string LoginPassword { get; set; } = string.Empty;
|
||||||
public string PasswordSalt { get; set; } = string.Empty;
|
public string PasswordSalt { get; set; } = string.Empty;
|
||||||
public Int64 PropertyID { get; set; }
|
public Int32 PropertyID { get; set; }
|
||||||
|
public Int32 MemberID { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserResponseModel
|
public class UserResponseModel
|
||||||
@@ -73,25 +77,25 @@ namespace APISocietyToolBox.Models
|
|||||||
|
|
||||||
public class UserRequestModel
|
public class UserRequestModel
|
||||||
{
|
{
|
||||||
|
public Int32? UserID { get; set; }
|
||||||
public string UserName { get; set; } = string.Empty;
|
public string UserName { get; set; } = string.Empty;
|
||||||
public string LoginName { get; set; } = string.Empty;
|
public string LoginName { get; set; } = string.Empty;
|
||||||
public string LoginPassword { get; set; } = string.Empty;
|
public string LoginPassword { get; set; } = string.Empty;
|
||||||
public string UserRole { get; set; } = string.Empty;
|
public string UserRole { get; set; } = string.Empty;
|
||||||
public int UserRoleID { get; set; }
|
public int UserRoleID { get; set; }
|
||||||
public int PropertyID { get; set; }
|
public int PropertyID { get; set; }
|
||||||
public string? RefreshToken { get; set; } = string.Empty;
|
public Int32 MemberID { get; set; }
|
||||||
public DateTime? TokenCreated { get; set; }
|
|
||||||
public DateTime? TokenExpires { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserTokens
|
public class UserTokens
|
||||||
{
|
{
|
||||||
[Key]
|
[Key]
|
||||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||||
public Int64 UserTokenID { get; set; }
|
public Int32 UserTokenID { get; set; }
|
||||||
public Int64? UserID { get; set; }
|
public Int64? UserID { get; set; }
|
||||||
public Int64? UserRoleID { get; set; }
|
public Int32? UserRoleID { get; set; }
|
||||||
public Int64? PropertyID { get; set; }
|
public Int32? PropertyID { get; set; }
|
||||||
public string? RefreshToken { get; set; }
|
public string? RefreshToken { get; set; }
|
||||||
public DateTime? TokenExpires { get; set; }
|
public DateTime? TokenExpires { get; set; }
|
||||||
public DateTime? TokenCreated { get; set; }
|
public DateTime? TokenCreated { get; set; }
|
||||||
@@ -106,5 +110,6 @@ namespace APISocietyToolBox.Models
|
|||||||
public int UserID { get; set; }
|
public int UserID { get; set; }
|
||||||
public string UserName { get; set; } = string.Empty;
|
public string UserName { get; set; } = string.Empty;
|
||||||
public int PropertyID { get; set; }
|
public int PropertyID { get; set; }
|
||||||
|
public int MemberID { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user