From 323a46a6239a4354652850e8e4745a1b11d28ec4 Mon Sep 17 00:00:00 2001 From: mahan Date: Sat, 6 Dec 2025 16:24:11 +0330 Subject: [PATCH] Add Program Manager menu item and implement JWT token generation for SSO login --- .../Areas/AdminNew/Pages/Index.cshtml.cs | 50 +++++++++++++++++++ .../Areas/AdminNew/Pages/Shared/_Menu.cshtml | 9 ++++ ServiceHost/appsettings.Development.json | 8 ++- ServiceHost/appsettings.json | 11 ++-- 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/ServiceHost/Areas/AdminNew/Pages/Index.cshtml.cs b/ServiceHost/Areas/AdminNew/Pages/Index.cshtml.cs index 6038fd16..974be9d0 100644 --- a/ServiceHost/Areas/AdminNew/Pages/Index.cshtml.cs +++ b/ServiceHost/Areas/AdminNew/Pages/Index.cshtml.cs @@ -7,6 +7,10 @@ using Company.Domain.WorkshopAccountAgg; using CompanyManagment.App.Contracts.Workshop; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.IdentityModel.Tokens; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Text; using WorkFlow.Application.Contracts.AdminWorkFlow; namespace ServiceHost.Areas.AdminNew.Pages @@ -121,5 +125,51 @@ namespace ServiceHost.Areas.AdminNew.Pages data = checkerCount, }); } + + public IActionResult OnGetProgramManager() + { + try + { + // دریافت اطلاعات کاربر فعلی + var currentAccountId = _authHelper.CurrentAccountId(); + var accountInfo = _authHelper.CurrentAccountInfo(); + + // تعریف Secret Key برای JWT (باید در appsettings.json تعریف شود) + var secretKey = _configuration["JwtSettings:SecretKey"] ?? ">3£>^1UBG@yw)QdhRC3$£:;r8~?qpp^oKK4D3a~8L2>enF;lkgh"; + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); + var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); + + // ایجاد Claims + var claims = new[] + { + new Claim(ClaimTypes.NameIdentifier, currentAccountId.ToString()), + new Claim(ClaimTypes.Name, accountInfo.Fullname ?? ""), + new Claim(ClaimTypes.Email, accountInfo.Username ?? ""), + new Claim("AccountId", currentAccountId.ToString()), + new Claim("RoleId", accountInfo.RoleId.ToString()) + }; + + // ایجاد JWT Token + var token = new JwtSecurityToken( + issuer: _configuration["JwtSettings:Issuer"] ?? "GozareshgirApp", + audience: _configuration["JwtSettings:Audience"] ?? "GozareshgirUsers", + claims: claims, + expires: DateTime.UtcNow.AddMinutes(int.Parse(_configuration["JwtSettings:ExpirationMinutes"] ?? "30")), + signingCredentials: credentials + ); + + var tokenString = new JwtSecurityTokenHandler().WriteToken(token); + var domain = _configuration["GozareshgirProgramManager:Domain"] ?? "http://localhost:5000"; + // Redirect به SSO-Login با Token + // var ssoUrl = $"https://pm{domain}/sso-login?token={Uri.EscapeDataString(tokenString)}"; + var ssoUrl = $"https://localhost:7032/api/Auth/sso-login?token={Uri.EscapeDataString(tokenString)}"; + return Redirect(ssoUrl); + } + catch (Exception ex) + { + // در صورت خطا، برگشت به صفحه اصلی + return RedirectToPage("/Index"); + } + } } } diff --git a/ServiceHost/Areas/AdminNew/Pages/Shared/_Menu.cshtml b/ServiceHost/Areas/AdminNew/Pages/Shared/_Menu.cshtml index f6ec0b36..8c2ea27d 100644 --- a/ServiceHost/Areas/AdminNew/Pages/Shared/_Menu.cshtml +++ b/ServiceHost/Areas/AdminNew/Pages/Shared/_Menu.cshtml @@ -629,6 +629,15 @@ +
  • + + + +
  • +
  • diff --git a/ServiceHost/appsettings.Development.json b/ServiceHost/appsettings.Development.json index 968961a6..fcfa13d3 100644 --- a/ServiceHost/appsettings.Development.json +++ b/ServiceHost/appsettings.Development.json @@ -48,7 +48,13 @@ //, "09116067106", "09114221321" ] }, - "SepehrGateWayTerminalId": 99213700 + "SepehrGateWayTerminalId": 99213700, + "JwtSettings": { + "SecretKey": ">3£>^1UBG@yw)QdhRC3$£:;r8~?qpp^oKK4D3a~8L2>enF;lkgh", + "Issuer": "GozareshgirApp", + "Audience": "GozareshgirUsers", + "ExpirationMinutes": 30 + } } diff --git a/ServiceHost/appsettings.json b/ServiceHost/appsettings.json index 0b602bb1..46613e68 100644 --- a/ServiceHost/appsettings.json +++ b/ServiceHost/appsettings.json @@ -7,7 +7,7 @@ } }, "ConnectionStrings": { - //"MesbahDb": "Data Source=.\\MSSQLSERVER2019;Initial Catalog=mesbah_db;Persist Security Info=False;User ID=mesbah_db;Password=sa142857$@;" + //"MesbahDb": "Data Source=.\\MSSQLSERVER2019;Initial Catalog=mesbah_db;Persist Security Info=False;User ID=mesbah_db;Password=sa142857$@;" "MesbahDb": "Data Source=.;Initial Catalog=mesbah_db;Integrated Security=True;TrustServerCertificate=true;", //dad-mehr @@ -36,6 +36,11 @@ "IsTestMode": false, "TestNumbers": [] }, - "SepehrGateWayTerminalId": 99213700 - + "SepehrGateWayTerminalId": 99213700, + "JwtSettings": { + "SecretKey": ">3£>^1UBG@yw)QdhRC3$£:;r8~?qpp^oKK4D3a~8L2>enF;lkgh", + "Issuer": "GozareshgirApp", + "Audience": "GozareshgirUsers", + "ExpirationMinutes": 30 + } }