first commit -push

This commit is contained in:
dungtt
2025-10-15 15:15:53 +07:00
parent 674ae395be
commit a9577c5756
885 changed files with 74595 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
// IdentityService.cs - Tạo dịch vụ này để tránh lỗi DbContext
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RobotNet.IdentityServer.Data;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RobotNet.IdentityServer.Services;
public class IdentityService
{
private readonly IServiceProvider _serviceProvider;
public IdentityService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public async Task<ApplicationUser?> GetUserByIdAsync(string userId)
{
using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var user = await userManager.Users.AsNoTracking().FirstOrDefaultAsync(u => u.Id == userId);
return user;
}
public async Task<ApplicationUser?> GetUserByNameAsync(string userName)
{
using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
return await userManager.FindByNameAsync(userName);
}
public async Task<List<string>> GetUserRolesAsync(ApplicationUser user)
{
using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roles = await userManager.GetRolesAsync(user);
return roles.ToList();
}
public async Task<IdentityResult> UpdateUserAsync(ApplicationUser user)
{
using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var existingUser = await userManager.FindByIdAsync(user.Id);
if (existingUser != null)
{
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Entry(existingUser).State = EntityState.Detached;
}
return await userManager.UpdateAsync(user);
}
public async Task<IdentityResult> ChangePasswordAsync(ApplicationUser user, string currentPassword, string newPassword)
{
using var scope = _serviceProvider.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
return await userManager.ChangePasswordAsync(user, currentPassword, newPassword);
}
}

View File

@@ -0,0 +1,61 @@
using MudBlazor;
namespace RobotNet.IdentityServer.Services;
public class PasswordStrengthService
{
/// <summary>
/// Đánh giá độ mạnh của mật khẩu (thang điểm 0-100)
/// </summary>
/// <param name="password">Mật khẩu cần đánh giá</param>
/// <returns>Điểm đánh giá từ 0-100</returns>
public int EvaluatePasswordStrength(string password)
{
if (string.IsNullOrEmpty(password))
return 0;
int strength = 0;
// Đánh giá dựa trên độ dài
if (password.Length >= 1) strength += 5;
if (password.Length >= 3) strength += 5;
if (password.Length >= 6) strength += 10;
if (password.Length >= 8) strength += 10;
if (password.Length >= 10) strength += 10;
// Đánh giá dựa trên độ phức tạp
if (password.Any(char.IsUpper)) strength += 15;
if (password.Any(char.IsLower)) strength += 15;
if (password.Any(char.IsDigit)) strength += 15;
if (password.Any(c => !char.IsLetterOrDigit(c))) strength += 15;
return System.Math.Min(strength, 100);
}
/// <summary>
/// Lấy màu tương ứng với độ mạnh của mật khẩu
/// </summary>
/// <param name="strength">Điểm đánh giá độ mạnh (0-100)</param>
/// <returns>Color tương ứng</returns>
public Color GetStrengthColor(int strength)
{
if (strength < 30) return Color.Error;
if (strength < 60) return Color.Warning;
if (strength < 80) return Color.Info;
return Color.Success;
}
/// <summary>
/// Lấy mô tả tương ứng với độ mạnh của mật khẩu
/// </summary>
/// <param name="strength">Điểm đánh giá độ mạnh (0-100)</param>
/// <returns>Mô tả dạng văn bản</returns>
public string GetStrengthDescription(int strength)
{
if (strength == 0) return "Chưa nhập mật khẩu";
if (strength < 30) return "Mật khẩu yếu";
if (strength < 60) return "Mật khẩu trung bình";
if (strength < 80) return "Mật khẩu tốt";
return "Mật khẩu mạnh";
}
}

View File

@@ -0,0 +1,26 @@
using System.IO;
using System.Threading.Tasks;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;
namespace RobotNet.IdentityServer.Services;
public class UserImageService
{
public async Task<(byte[] ImageBytes, string ContentType)> ResizeAndConvertAsync(Stream input)
{
using var image = await Image.LoadAsync(input);
image.Mutate(x => x.Resize(new ResizeOptions
{
Size = new Size(300, 300),
Mode = ResizeMode.Crop
}));
using var ms = new MemoryStream();
await image.SaveAsJpegAsync(ms, new JpegEncoder { Quality = 90 });
return (ms.ToArray(), "image/jpeg");
}
}

View File

@@ -0,0 +1,41 @@
namespace RobotNet.IdentityServer.Services;
public class UserInfoService
{
private readonly List<Func<Task>> _handlers = [];
public void RegisterHandler(Func<Task> handler)
{
if (handler != null && !_handlers.Contains(handler))
{
_handlers.Add(handler);
}
}
public void UnregisterHandler(Func<Task> handler)
{
if (handler != null && _handlers.Contains(handler))
{
_handlers.Remove(handler);
}
}
public async Task NotifyUserInfoChanged()
{
var handlers = new List<Func<Task>>(_handlers);
foreach (var handler in handlers)
{
try
{
await handler();
}
catch (Exception ex)
{
Console.WriteLine($"Error in user info change handler: {ex.Message}");
}
}
}
}