-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1472e6
commit b7265f3
Showing
10 changed files
with
207 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=AnalysisData_002FResources/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using AnalysisData.Services; | ||
using AnalysisData.UserManage.LoginModel; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace AnalysisData.Controllers; | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class UserController : ControllerBase | ||
{ | ||
private readonly IUserService _userService; | ||
|
||
public UserController(IUserService userService) | ||
{ | ||
_userService = userService; | ||
} | ||
[HttpPost("login")] | ||
public IActionResult Login([FromBody] UserLoginModel userLoginModel) | ||
{ | ||
var userRoles = _userService.Login(userLoginModel); | ||
return Ok(new { roles = userRoles }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using AnalysisData.CookieSevice.abstractions; | ||
|
||
namespace AnalysisData.CookieSevice; | ||
|
||
public class CookieService : ICookieService | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public CookieService(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public void SetCookie(string name, string token,bool rememberMe) | ||
{ | ||
var options = new CookieOptions | ||
{ | ||
HttpOnly = true, | ||
Secure = true, | ||
SameSite = SameSiteMode.Strict | ||
}; | ||
|
||
if (rememberMe) | ||
{ | ||
options.Expires = DateTimeOffset.UtcNow.AddDays(7); | ||
} | ||
else | ||
{ | ||
options.IsEssential = true; | ||
} | ||
|
||
_httpContextAccessor.HttpContext.Response.Cookies.Append(name, token, options); | ||
} | ||
|
||
public string GetCookie(string name) | ||
{ | ||
_httpContextAccessor.HttpContext.Request.Cookies.TryGetValue(name, out var value); | ||
return value; | ||
} | ||
|
||
public void RemoveCookie(string name) | ||
{ | ||
_httpContextAccessor.HttpContext.Response.Cookies.Delete(name); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
AnalysisData/AnalysisData/CookieService/abstractions/ICookieService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace AnalysisData.CookieSevice.abstractions; | ||
|
||
public interface ICookieService | ||
{ | ||
void RemoveCookie(string name); | ||
string GetCookie(string name); | ||
void SetCookie(string name, string token,bool rememberMe); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
AnalysisData/AnalysisData/JwtService/abstractions/IJwtService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace AnalysisData.JwtService; | ||
|
||
public interface IJwtService | ||
{ | ||
Task<string> GenerateJwtToken(string userName); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<root> | ||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | ||
<xsd:element name="root" msdata:IsDataSet="true"> | ||
|
||
</xsd:element> | ||
</xsd:schema> | ||
<resheader name="resmimetype"> | ||
<value>text/microsoft-resx</value> | ||
</resheader> | ||
<resheader name="version"> | ||
<value>1.3</value> | ||
</resheader> | ||
<resheader name="reader"> | ||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<resheader name="writer"> | ||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<data name="NotFoundUserException" xml:space="preserve"> | ||
<value>user not found ! </value> | ||
</data> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using AnalysisData.UserManage.LoginModel; | ||
|
||
namespace AnalysisData.Services; | ||
|
||
public interface IUserService | ||
{ | ||
Task<List<string>> Login(UserLoginModel userLoginModel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,44 @@ | ||
using AnalysisData.CookieSevice.abstractions; | ||
using AnalysisData.Data; | ||
using AnalysisData.JwtService; | ||
using AnalysisData.Repository.RoleRepository.Abstraction; | ||
using AnalysisData.Repository.UserRepository.Abstraction; | ||
using AnalysisData.UserManage.LoginModel; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace AnalysisData.Services; | ||
|
||
public class UserService : ControllerBase | ||
public class UserService : IUserService | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
private readonly ICookieService _cookieService; | ||
private readonly IJwtService _jwtService; | ||
|
||
public UserService(IUserRepository userRepository) | ||
public UserService(IUserRepository userRepository, ICookieService cookieService,IJwtService jwtService) | ||
{ | ||
_userRepository = userRepository; | ||
_cookieService = cookieService; | ||
_jwtService = jwtService; | ||
} | ||
public async Task<IActionResult> Login([FromBody] UserLoginModel userLoginModel) | ||
public async Task<List<string>> Login(UserLoginModel userLoginModel) | ||
{ | ||
var user = await _userRepository.GetUser(userLoginModel.userName); | ||
if (user is null) | ||
if (userLoginModel.userName.Length > 255) | ||
{ | ||
return NotFound($"Unable to load user with username '{userLoginModel.userName}'"); | ||
return null; | ||
} | ||
|
||
if (user.Password != userLoginModel.password) | ||
var user = await _userRepository.GetUser(userLoginModel.userName); | ||
if (user == null || user.Password != userLoginModel.password) | ||
{ | ||
return Unauthorized("Invalid username or password."); | ||
return null; | ||
} | ||
var token = await _jwtService.GenerateJwtToken(userLoginModel.userName); | ||
|
||
return Ok(new | ||
{ | ||
Massage = "welcome", | ||
Token = "سشی" | ||
}); | ||
|
||
_cookieService.SetCookie("AuthToken", token, userLoginModel.rememberMe); | ||
|
||
var roles = user.UserRoles.Select(ur => ur.Role.RoleName).ToList(); | ||
|
||
return roles; | ||
} | ||
|
||
} |