This commit is contained in:
2026-10-19 00:39:49 +03:30
parent 91b2586610
commit 6a7bdddabd
4 changed files with 4603 additions and 0 deletions
@@ -0,0 +1,941 @@
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>پیاده‌سازی سرویس احراز هویت و تعیین سطوح دسترسی | استک .NET</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 950px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge { display: flex; align-items: center; gap: 12px; }
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta { text-align: left; font-size: 12px; opacity: 0.9; }
.doc-meta div { margin-bottom: 3px; }
.document-title { text-align: center; position: relative; z-index: 1; padding: 20px 0; }
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 { font-size: 28px; font-weight: 800; margin-bottom: 10px; letter-spacing: -0.5px; }
.document-title h2 { font-size: 16px; font-weight: 400; opacity: 0.9; }
.document-body { padding: 40px; }
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section { margin-bottom: 40px; page-break-inside: avoid; }
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 { font-size: 19px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.section-title .subtitle { font-size: 12px; color: var(--text-muted); }
.section-content { padding-right: 10px; }
.section-content p { margin-bottom: 12px; text-align: justify; text-indent: 20px; }
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong { color: var(--accent-dark); }
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead { background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: white; }
.styled-table th { padding: 14px 16px; text-align: right; font-weight: 600; font-size: 13px; }
.styled-table td { padding: 13px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row { background: #fffaf0 !important; font-weight: 600; }
.comparison-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 25px 0; }
.comparison-card {
background: #f8fafc;
border: 2px solid var(--border);
border-radius: 10px;
padding: 20px;
position: relative;
}
.comparison-card.recommended { border-color: var(--success); background: linear-gradient(to left, #f0fff4, #e6fffa); }
.comparison-card .card-badge {
position: absolute;
top: -10px; right: 20px;
background: var(--success);
color: white;
padding: 3px 12px;
border-radius: 15px;
font-size: 11px;
font-weight: 600;
}
.comparison-card h4 {
font-size: 16px;
font-weight: 700;
color: var(--primary);
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid var(--border);
}
.comparison-card.recommended h4 { border-bottom-color: var(--success); }
.comparison-card ul { list-style: none; padding: 0; }
.comparison-card ul li {
padding: 8px 0;
border-bottom: 1px solid var(--border);
font-size: 13px;
display: flex;
justify-content: space-between;
}
.comparison-card ul li:last-child { border-bottom: none; }
.comparison-card ul li .value { font-weight: 600; color: var(--primary); }
.quote-box {
background: linear-gradient(135deg, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin: 20px 0;
position: relative;
}
.quote-box::before {
content: '"';
position: absolute;
top: -10px; right: 15px;
font-size: 60px;
color: var(--accent);
opacity: 0.3;
font-family: Georgia, serif;
}
.quote-box p { font-style: italic; color: var(--text-muted); margin-bottom: 10px; text-indent: 0 !important; }
.quote-box .source { font-size: 12px; color: var(--text-muted); text-align: left; font-style: normal; }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; margin: 25px 0; }
.stat-card {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
box-shadow: 0 4px 12px rgba(26,54,93,0.2);
}
.stat-card .stat-value { font-size: 24px; font-weight: 800; margin-bottom: 5px; color: var(--accent); line-height: 1.3; }
.stat-card .stat-label { font-size: 12px; opacity: 0.9; }
.reference-list { list-style: none; padding: 0; margin: 20px 0; }
.reference-list li {
background: #f8fafc;
border-right: 3px solid var(--primary-light);
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 6px;
font-size: 13px;
}
.reference-list li strong { color: var(--primary); display: block; margin-bottom: 5px; }
.reference-list li .ref-source { font-size: 11px; color: var(--text-muted); margin-top: 5px; }
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 { font-size: 22px; font-weight: 800; margin-bottom: 15px; color: var(--accent); }
.conclusion-box p { font-size: 14px; line-height: 1.8; text-indent: 0 !important; margin-bottom: 15px; }
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand { font-weight: 700; color: var(--accent); margin-bottom: 5px; font-size: 14px; }
.footer-divider { width: 60px; height: 2px; background: var(--accent); margin: 10px auto; }
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
.step-list { list-style: none; padding: 0; margin: 20px 0; counter-reset: step; }
.step-list > li {
position: relative;
padding: 16px 60px 16px 20px;
margin-bottom: 12px;
background: #f8fafc;
border-radius: 8px;
border-right: 4px solid var(--accent);
font-size: 13.5px;
counter-increment: step;
}
.step-list > li::before {
content: counter(step);
position: absolute;
right: 15px;
top: 16px;
width: 30px; height: 30px;
background: var(--primary);
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
font-size: 13px;
}
.step-list > li strong { color: var(--primary); display: block; margin-bottom: 4px; }
.code-snippet {
background: #1a202c;
color: #e2e8f0;
padding: 14px 18px;
border-radius: 8px;
font-family: 'Consolas', 'Courier New', monospace;
font-size: 12px;
line-height: 1.7;
direction: ltr;
text-align: left;
overflow-x: auto;
margin: 12px 0;
border-right: 4px solid var(--accent);
}
.code-snippet .cm { color: #718096; }
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.comparison-grid { grid-template-columns: 1fr; }
.stats-grid { grid-template-columns: 1fr; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: ARCH-1405-004</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: مهر ۱۴۰۵</div>
<div>طبقه‌بندی: محرمانه</div>
</div>
</div>
<div class="document-title">
<span class="label">IMPLEMENTATION GUIDE</span>
<h1>پیاده‌سازی سرویس احراز هویت و تعیین سطوح دسترسی</h1>
<h2>راهنمای گام‌به‌گام عملیاتی بر بستر استک .NET</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>کارفرمای محترم،</strong><br>
این مستند به‌عنوان راهنمای عملیاتی پیاده‌سازی سرویس احراز هویت و تعیین سطوح دسترسی بر بستر استک .NET تهیه شده است. هدف، ارائه مسیر گام‌به‌گام از انتخاب ابزار و معماری تا اتصال کلاینت‌ها، معرفی کتابخانه‌های کلیدی و ترسیم نقشه راه شفاف برای اجرا است.
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>خلاصه اجرایی</h3>
<div class="subtitle">Executive Summary</div>
</div>
</div>
<div class="section-content">
<p>پیاده‌سازی سرویس احراز هویت و تعیین سطوح دسترسی در استک .NET، امروزه با انتخاب میان دو رویکرد اصلی انجام می‌شود: استفاده از <strong>ASP.NET Core Identity</strong> به‌صورت توکار برای سامانه‌های یکپارچه، یا پیاده‌سازی یک <strong>سرور OAuth 2.0 / OpenID Connect</strong> کامل با <strong>OpenIddict</strong> یا <strong>Duende IdentityServer</strong> برای سناریوهای چندکلاینتی و فدرال. انتخاب صحیح، تابع معماری کلان سامانه و الزامات سازمانی است.</p>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">گام‌به‌گام</div>
<div class="stat-label">مسیر اجرایی شفاف از صفر تا استقرار</div>
</div>
<div class="stat-card">
<div class="stat-value">استاندارد</div>
<div class="stat-label">اتکا به OAuth 2.0 / OIDC</div>
</div>
<div class="stat-card">
<div class="stat-value">قابل توسعه</div>
<div class="stat-label">معماری ماژولار و مستقل از کلاینت</div>
</div>
</div>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> با انتخاب ابزار مناسب، پیاده‌سازی گام‌به‌گام و رعایت اصول امنیتی، سرویس احراز هویت و مجوزدهی در .NET می‌تواند به‌صورت کامل درون کدبیس سازمانی، بدون وابستگی به سرویس‌های ابری خارجی، مستقر و مدیریت شود.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>انتخاب ابزار و معماری پایه</h3>
<div class="subtitle">Tooling & Architecture Decision</div>
</div>
</div>
<div class="section-content">
<p>پیش از ورود به مراحل پیاده‌سازی، لازم است رویکرد معماری و ابزار پایه انتخاب شود. در استک .NET، سه گزینه اصلی وجود دارد که هر یک برای سناریوی مشخصی مناسب است:</p>
<table class="styled-table">
<thead>
<tr>
<th>گزینه</th>
<th>نوع</th>
<th>سناریوی مناسب</th>
<th>هزینه</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>ASP.NET Core Identity API Endpoints</strong></td>
<td>توکار (Built-in)</td>
<td>سامانه تک‌دامنه با فرانت و بک‌اند یکپارچه</td>
<td>رایگان (MIT)</td>
</tr>
<tr>
<td><strong>OpenIddict</strong></td>
<td>کتابخانه / فریم‌ورک</td>
<td>سرور OAuth 2.0/OIDC سفارشی، SaaS، چندکلاینتی</td>
<td>رایگان (Apache 2.0)</td>
</tr>
<tr>
<td><strong>Duende IdentityServer</strong></td>
<td>محصول تجاری</td>
<td>سازمان‌های بزرگ با نیاز به پشتیبانی تجاری</td>
<td>لایسنس سالانه</td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 توصیه معماری:</strong> برای سامانه‌هایی که نیاز به سرور احراز هویت مستقل، پشتیبانی از چندین کلاینت (وب، موبایل، سرویس‌های ماشین‌به‌ماشین) و کنترل کامل بر جریان‌های OAuth 2.0/OIDC دارند، <strong>OpenIddict</strong> انتخاب متوازن و مقرون‌به‌صرفه‌ای است [citation:5]. برای سامانه‌های ساده تک‌دامنه، <strong>ASP.NET Core Identity API Endpoints</strong> از .NET 8 به بعد کفایت می‌کند [citation:5].
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>پیاده‌سازی گام‌به‌گام سرویس‌دهنده</h3>
<div class="subtitle">Step-by-Step Server Implementation</div>
</div>
</div>
<div class="section-content">
<p>مراحل زیر بر مبنای پیاده‌سازی یک سرور احراز هویت و مجوزدهی با <strong>OpenIddict</strong> در ASP.NET Core شرح داده می‌شود. این مراحل قابل انطباق با Duende IdentityServer نیز هستند.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۱: ایجاد پروژه و نصب بسته‌ها</h4>
<p>یک پروژه ASP.NET Core (ترجیحاً با قالب Web API یا Empty) ایجاد کنید و بسته‌های NuGet زیر را نصب نمایید:</p>
<div class="code-snippet">
dotnet add package OpenIddict.AspNetCore --version 7.x<br>
dotnet add package OpenIddict.EntityFrameworkCore --version 7.x<br>
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
</div>
<p>بسته <code>OpenIddict.AspNetCore</code> امکانات سرور، اعتبارسنجی و کلاینت را فراهم می‌کند و <code>OpenIddict.EntityFrameworkCore</code> ذخیره‌سازی داده‌های OpenIddict را در EF Core ممکن می‌سازد [citation:19].</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۲: پیکربندی DbContext و OpenIddict Core</h4>
<p>در فایل <code>Program.cs</code>، ابتدا <code>DbContext</code> را برای ASP.NET Core Identity و OpenIddict پیکربندی کنید:</p>
<div class="code-snippet">
builder.Services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;<br>
{<br>
&nbsp;&nbsp;options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));<br>
&nbsp;&nbsp;options.UseOpenIddict();<br>
});<br><br>
builder.Services.AddIdentity&lt;IdentityUser, IdentityRole&gt;()<br>
&nbsp;&nbsp;.AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()<br>
&nbsp;&nbsp;.AddDefaultTokenProviders();
</div>
<p>فراخوانی <code>UseOpenIddict()</code> جداول موردنیاز OpenIddict را به مدل داده اضافه می‌کند. همچنین <code>AddIdentity</code> زیرساخت مدیریت کاربران و نقش‌ها را فراهم می‌سازد [citation:19][citation:1].</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۳: پیکربندی سرور OpenIddict</h4>
<p>سرویس‌های سرور OpenIddict را با تنظیم Endpointها، جریان‌های مجاز و گواهی‌های امضا پیکربندی کنید:</p>
<div class="code-snippet">
builder.Services.AddOpenIddict()<br>
&nbsp;&nbsp;.AddCore(options =&gt; options.UseEntityFrameworkCore().UseDbContext&lt;ApplicationDbContext&gt;())<br>
&nbsp;&nbsp;.AddServer(options =&gt;<br>
&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.SetAuthorizationEndpointUris("connect/authorize")<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetTokenEndpointUris("connect/token");<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;options.AllowAuthorizationCodeFlow()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.AllowRefreshTokenFlow();<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;options.AddDevelopmentSigningCertificate();<br>
&nbsp;&nbsp;&nbsp;&nbsp;options.UseAspNetCore().EnableAuthorizationEndpointPassthrough();<br>
&nbsp;&nbsp;});
</div>
<p><code>EnableAuthorizationEndpointPassthrough()</code> به شما اجازه می‌دهد منطق احراز هویت کاربر (صفحه ورود) را در کنترلر یا Minimal API خود پیاده‌سازی کنید [citation:2].</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۴: پیاده‌سازی کنترلر مجوز (Authorization Controller)</h4>
<p>یک کنترلر برای مدیریت Endpoint مجوز و صدور توکن ایجاد کنید. این کنترلر مسئول احراز هویت کاربر و ساخت <code>ClaimsIdentity</code> است:</p>
<div class="code-snippet">
[HttpPost("~/connect/token"), Produces("application/json")]<br>
public async Task&lt;IActionResult&gt; Exchange()<br>
{<br>
&nbsp;&nbsp;var request = HttpContext.GetOpenIddictServerRequest();<br>
&nbsp;&nbsp;if (request.IsAuthorizationCodeGrantType())<br>
&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;// اعتبارسنجی کد مجوز و ساخت هویت<br>
&nbsp;&nbsp;&nbsp;&nbsp;var identity = new ClaimsIdentity(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TokenValidationParameters.DefaultAuthenticationType,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Claims.Name, Claims.Role);<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;identity.AddClaim(Claims.Subject, userId);<br>
&nbsp;&nbsp;&nbsp;&nbsp;identity.AddClaim(Claims.Name, userName);<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;return SignIn(new ClaimsPrincipal(identity),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);<br>
&nbsp;&nbsp;}<br>
&nbsp;&nbsp;throw new InvalidOperationException("Unsupported grant type.");<br>
}
</div>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۵: پیکربندی Middleware و مسیرها</h4>
<p>ترتیب Middleware احراز هویت و مجوزدهی بسیار حیاتی است. <code>UseAuthentication()</code> باید پس از <code>UseRouting()</code> و پیش از <code>UseEndpoints()</code> قرار گیرد [citation:2]:</p>
<div class="code-snippet">
app.UseRouting();<br>
app.UseCors();<br>
app.UseAuthentication();<br>
app.UseAuthorization();<br>
app.UseEndpoints(endpoints =&gt; endpoints.MapControllers());
</div>
<div class="success-box">
<strong>✓ نتیجه گام‌های سرور:</strong> پس از طی این پنج گام، یک سرور احراز هویت و مجوزدهی فعال خواهید داشت که می‌تواند توکن‌های OAuth 2.0/OIDC صادر کند. گام بعدی، اتصال کلاینت‌ها به این سرور است.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>اتصال کلاینت‌ها به سرویس احراز هویت</h3>
<div class="subtitle">Client Integration Guide</div>
</div>
</div>
<div class="section-content">
<p>کلاینت‌ها بر اساس نوع خود (وب، موبایل، سرویس ماشین‌به‌ماشین) از جریان‌های متفاوتی استفاده می‌کنند. در ادامه، اتصال دو نوع کلاینت رایج شرح داده می‌شود.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">الف) کلاینت وب (ASP.NET Core MVC / Razor Pages)</h4>
<p>برای کلاینت وب، از پکیج <code>Microsoft.AspNetCore.Authentication.OpenIdConnect</code> استفاده کنید. پیکربندی در <code>Program.cs</code> به‌صورت زیر است:</p>
<div class="code-snippet">
builder.Services.AddAuthentication(options =&gt;<br>
{<br>
&nbsp;&nbsp;options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;<br>
&nbsp;&nbsp;options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;<br>
})<br>
.AddCookie()<br>
.AddOpenIdConnect(options =&gt;<br>
{<br>
&nbsp;&nbsp;options.Authority = "https://localhost:5001";<br>
&nbsp;&nbsp;options.ClientId = "web-client";<br>
&nbsp;&nbsp;options.ClientSecret = "secret";<br>
&nbsp;&nbsp;options.ResponseType = "code";<br>
&nbsp;&nbsp;options.Scope.Add("openid");<br>
&nbsp;&nbsp;options.Scope.Add("profile");<br>
&nbsp;&nbsp;options.SaveTokens = true;<br>
});
</div>
<p>این پیکربندی، جریان Authorization Code را فعال می‌کند که برای کلاینت‌های وب با بک‌اند امن، توصیه‌شده‌ترین جریان است.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">ب) سرویس ماشین‌به‌ماشین (Client Credentials)</h4>
<p>برای سرویس‌هایی که بدون دخالت کاربر به API دسترسی نیاز دارند، از جریان Client Credentials استفاده کنید. با پکیج <code>IdentityModel</code>:</p>
<div class="code-snippet">
var client = new HttpClient();<br>
var disco = await client.GetDiscoveryDocumentAsync("https://localhost:5001");<br><br>
var tokenResponse = await client.RequestClientCredentialsTokenAsync(<br>
&nbsp;&nbsp;new ClientCredentialsTokenRequest<br>
&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;Address = disco.TokenEndpoint,<br>
&nbsp;&nbsp;&nbsp;&nbsp;ClientId = "machine-client",<br>
&nbsp;&nbsp;&nbsp;&nbsp;ClientSecret = "secret",<br>
&nbsp;&nbsp;&nbsp;&nbsp;Scope = "api1"<br>
&nbsp;&nbsp;});
</div>
<p>پس از دریافت توکن، آن را در هدر <code>Authorization: Bearer {token}</code> به API ارسال کنید.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">ج) کلاینت SPA (React / Angular / Blazor WASM)</h4>
<p>برای SPAها، جریان Authorization Code با PKCE توصیه می‌شود. در سمت سرور، <code>AllowAuthorizationCodeFlow()</code> و <code>RequireProofKeyForCodeExchange()</code> را فعال کنید. در سمت کلاینت، از کتابخانه <code>oidc-client-ts</code> (برای JavaScript) یا <code>Microsoft.AspNetCore.Components.WebAssembly.Authentication</code> (برای Blazor WASM) استفاده نمایید.</p>
<div class="info-box">
<strong>💡 نکته امنیتی:</strong> هرگز از جریان Implicit در محیط عملیاتی استفاده نکنید. برای SPAها همیشه Authorization Code + PKCE را انتخاب کنید. برای کلاینت‌های محرمانه (وب با بک‌اند)، Client Secret را در سرور نگهداری کنید و هرگز آن را به فرانت‌اند منتقل نکنید.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>کتابخانه‌ها و منابع کلیدی</h3>
<div class="subtitle">Libraries & Resources</div>
</div>
</div>
<div class="section-content">
<p>جدول زیر کتابخانه‌های اصلی مورد استفاده در پیاده‌سازی را همراه با منبع رسمی معرفی می‌کند:</p>
<table class="styled-table">
<thead>
<tr>
<th>کتابخانه / منبع</th>
<th>نقش</th>
<th>منبع رسمی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>OpenIddict</strong></td>
<td>سرور OAuth 2.0 / OpenID Connect برای ASP.NET Core</td>
<td>documentation.openiddict.com [citation:19]</td>
</tr>
<tr>
<td><strong>ASP.NET Core Identity</strong></td>
<td>مدیریت کاربران، نقش‌ها و رمز عبور</td>
<td>learn.microsoft.com/aspnet/core/security/authentication/identity [citation:13]</td>
</tr>
<tr>
<td><strong>Duende IdentityServer</strong></td>
<td>سرور OIDC/OAuth 2.0 تجاری (جایگزین IdentityServer4)</td>
<td>docs.duendesoftware.com [citation:1][citation:7]</td>
</tr>
<tr>
<td><strong>Microsoft.AspNetCore.Authentication.OpenIdConnect</strong></td>
<td>اتصال کلاینت‌های .NET به سرور OIDC</td>
<td>learn.microsoft.com/aspnet/core/security/authentication [citation:13]</td>
</tr>
<tr>
<td><strong>IdentityModel</strong></td>
<td>کتابخانه کلاینت برای دریافت توکن و Discovery</td>
<td>github.com/IdentityModel/IdentityModel</td>
</tr>
<tr>
<td><strong>openiddict-samples</strong></td>
<td>نمونه‌های رسمی جریان‌های OAuth 2.0/OIDC</td>
<td>github.com/openiddict/openiddict-samples [citation:6]</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">منابع تکمیلی:</h4>
<ul class="reference-list">
<li>
<strong>مستندات رسمی OpenIddict — شروع سریع</strong>
راهنمای ایجاد سرور اختصاصی و پیکربندی Endpointها
<div class="ref-source">https://documentation.openiddict.com/guides/getting-started/creating-your-own-server-instance.html [citation:19]</div>
</li>
<li>
<strong>مستندات Duende IdentityServer — یکپارچه‌سازی با ASP.NET Core Identity</strong>
آموزش گام‌به‌گام اتصال IdentityServer به ASP.NET Core Identity
<div class="ref-source">https://docs.duendesoftware.com/identityserver/quickstarts/5-aspnetid/ [citation:1]</div>
</li>
<li>
<strong>مستندات Microsoft — مجوزدهی مبتنی بر Policy</strong>
راهنمای رسمی Policy-Based Authorization در ASP.NET Core
<div class="ref-source">https://learn.microsoft.com/aspnet/core/security/authorization/policies [citation:15]</div>
</li>
<li>
<strong>ASP.NET Zero — وضعیت احراز هویت .NET در ۲۰۲۶</strong>
مقایسه جامع OpenIddict، Keycloak، ASP.NET Identity و Azure Entra ID
<div class="ref-source">https://aspnetzero.com/blog/the-state-of-net-auth-in-2026 [citation:5]</div>
</li>
</ul>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>اجزاء و مؤلفه‌های موردنیاز</h3>
<div class="subtitle">Required Components</div>
</div>
</div>
<div class="section-content">
<p>معماری کامل سرویس احراز هویت و مجوزدهی در .NET از مؤلفه‌های زیر تشکیل می‌شود:</p>
<table class="styled-table">
<thead>
<tr>
<th>مؤلفه</th>
<th>نقش</th>
<th>ابزار / کتابخانه</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>سرور مجوز (Authorization Server)</strong></td>
<td>صدور توکن، مدیریت جریان‌های OAuth 2.0/OIDC</td>
<td>OpenIddict / Duende IdentityServer</td>
</tr>
<tr>
<td><strong>مخزن کاربران (User Store)</strong></td>
<td>ذخیره اطلاعات کاربران، نقش‌ها و Claims</td>
<td>ASP.NET Core Identity + EF Core</td>
</tr>
<tr>
<td><strong>سرور منبع (Resource Server)</strong></td>
<td>محافظت از APIها و اعتبارسنجی توکن</td>
<td>ASP.NET Core Web API + JWT Bearer</td>
</tr>
<tr>
<td><strong>کلاینت‌ها (Clients)</strong></td>
<td>وب، موبایل، SPA، سرویس ماشین‌به‌ماشین</td>
<td>OpenIdConnect Middleware / IdentityModel</td>
</tr>
<tr>
<td><strong>پایگاه داده (Database)</strong></td>
<td>ذخیره کاربران، کلاینت‌ها، توکن‌ها و مجوزها</td>
<td>SQL Server / PostgreSQL + EF Core</td>
</tr>
<tr>
<td><strong>گواهی امضا (Signing Certificate)</strong></td>
<td>امضای توکن‌ها و اطمینان از اصالت</td>
<td>X.509 Certificate (تولید یا purchased)</td>
</tr>
</tbody>
</table>
<div class="warning-box">
<strong>⚠️ هشدار عملیاتی:</strong> در محیط تولید، هرگز از <code>AddDevelopmentSigningCertificate()</code> استفاده نکنید. گواهی امضای معتبر (X.509) تهیه کرده و به‌صورت امن نگهداری کنید. همچنین کلیدهای امضا را به‌صورت متمرکز (مانند Azure Key Vault یا HashiCorp Vault) مدیریت نمایید.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>پیشنهادهای عملیاتی و بهبود توسعه</h3>
<div class="subtitle">Operational Recommendations & Improvements</div>
</div>
</div>
<div class="section-content">
<p>بر اساس تجربه پیاده‌سازی، پیشنهادهای زیر برای ارتقای کیفیت، امنیت و نگهداشت‌پذیری سرویس ارائه می‌شود:</p>
<ul class="step-list">
<li>
<strong>مدیریت متمرکز کلیدها (Key Management)</strong>
کلیدهای امضای توکن را در یک سرویس مدیریت کلید (مانند Azure Key Vault یا HashiCorp Vault) نگهداری کنید. چرخش دوره‌ای کلیدها را برنامه‌ریزی نمایید و از دسترسی مستقیم کد به کلیدها جلوگیری کنید.
</li>
<li>
<strong>استفاده از HTTPS اجباری</strong>
تمام Endpointهای سرویس احراز هویت باید صرفاً از طریق HTTPS در دسترس باشند. در OpenIddict، گزینه <code>DisableTransportSecurityRequirement()</code> را در تولید فعال نکنید [citation:2].
</li>
<li>
<strong>پیاده‌سازی Policy-Based Authorization</strong>
برای کنترل دسترسی دقیق‌تر، از Policyهای سفارشی به‌جای بررسی مستقیم نقش‌ها استفاده کنید. این رویکرد انعطاف‌پذیری بالاتری در تعریف قوانین دسترسی فراهم می‌کند [citation:15].
</li>
<li>
<strong>ثبت رویداد و حسابرسی (Audit Logging)</strong>
تمام رویدادهای ورود، صدور توکن، رد دسترسی و تغییر نقش را در یک سیستم متمرکز ثبت کنید. این امر برای تحلیل امنیتی و انطباق ضروری است.
</li>
<li>
<strong>پشتیبانی از Multi-Factor Authentication (MFA)</strong>
برای کاربران با دسترسی حساس، احراز هویت چندعاملی را فعال کنید. ASP.NET Core Identity از TOTP و Passkey پشتیبانی می‌کند [citation:7].
</li>
<li>
<strong>تست خودکار جریان‌های احراز هویت</strong>
برای هر جریان OAuth 2.0 (Authorization Code، Client Credentials، Refresh Token) تست یکپارچگی بنویسید تا از صحت عملکرد در برابر تغییرات آینده اطمینان حاصل شود.
</li>
<li>
<strong>پایش و هشدار (Monitoring & Alerting)</strong>
نرخ خطاهای ورود، درخواست‌های ناموفق توکن و تأخیر پاسخ Endpointها را پایش کرده و برای ناهنجاری‌ها هشدار تعریف کنید.
</li>
</ul>
<div class="success-box">
<strong>✓ نتیجه:</strong> رعایت این پیشنهادها، سرویس احراز هویت را از یک پیاده‌سازی صرفاً کاربردی به یک سرویس امن، قابل حسابرسی و آماده برای محیط تولید ارتقا می‌دهد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۸</div>
<div class="section-title">
<h3>نقشه راه اجرایی</h3>
<div class="subtitle">Implementation Roadmap</div>
</div>
</div>
<div class="section-content">
<p>نقشه راه زیر، مسیر اجرای پروژه را در چهار فاز اصلی با خروجی‌های مشخص ترسیم می‌کند:</p>
<table class="styled-table">
<thead>
<tr>
<th>فاز</th>
<th>فعالیت‌ها</th>
<th>خروجی قابل تحویل</th>
<th>مدت تقریبی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>فاز ۱: پایه‌ریزی</strong></td>
<td>ایجاد پروژه، نصب بسته‌ها، پیکربندی DbContext و Identity</td>
<td>پروژه قابل اجرا با DbContext و Identity فعال</td>
<td>۲–۳ روز</td>
</tr>
<tr>
<td><strong>فاز ۲: سرور مجوز</strong></td>
<td>پیکربندی OpenIddict، پیاده‌سازی Authorization Controller، Endpointهای توکن</td>
<td>سرور OIDC فعال با قابلیت صدور توکن</td>
<td>۳–۵ روز</td>
</tr>
<tr>
<td><strong>فاز ۳: کلاینت‌ها</strong></td>
<td>اتصال کلاینت وب، SPA و سرویس ماشین‌به‌ماشین، تست جریان‌ها</td>
<td>کلاینت‌های متصل و احراز هویت‌شده</td>
<td>۲–۴ روز</td>
</tr>
<tr>
<td><strong>فاز ۴: سخت‌سازی و استقرار</strong></td>
<td>مدیریت کلید، HTTPS اجباری، MFA، لاگ حسابرسی، استقرار در محیط تولید</td>
<td>سرویس آماده تولید با پایش و امنیت کامل</td>
<td>۳–۵ روز</td>
</tr>
<tr class="highlight-row">
<td><strong>جمع</strong></td>
<td>—</td>
<td><strong>سرویس احراز هویت و مجوزدهی کامل و عملیاتی</strong></td>
<td><strong>۱۰–۱۷ روز کاری</strong></td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 نکته برنامه‌ریزی:</strong> مدت‌زمان‌های ذکرشده برای یک تیم دو نفره با تجربه قبلی در ASP.NET Core و OAuth 2.0 تخمین زده شده است. در صورت آشنایی اولیه تیم با OpenIddict، فازها می‌توانند همپوشانی داشته باشند و زمان کل کاهش یابد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۹</div>
<div class="section-title">
<h3>جمع‌بندی نهایی</h3>
<div class="subtitle">Final Summary</div>
</div>
</div>
<div class="section-content">
<p>پیاده‌سازی سرویس احراز هویت و تعیین سطوح دسترسی بر بستر .NET، با انتخاب ابزار مناسب (OpenIddict یا ASP.NET Core Identity)، رعایت گام‌های مشخص و توجه به اصول امنیتی، مسیری شفاف و قابل اجرا دارد. این مستند با ارائه مراحل عملیاتی، معرفی کتابخانه‌ها و ترسیم نقشه راه، مبنای اجرایی لازم را فراهم می‌آورد.</p>
<div class="conclusion-box">
<h3>🎯 جمع‌بندی اجرایی</h3>
<p>سرویس احراز هویت و مجوزدهی، یک مؤلفه زیرساختی کلیدی در هر سامانه است که با پیاده‌سازی صحیح، امنیت، انطباق و تجربه کاربری را به‌طور هم‌زمان ارتقا می‌دهد.</p>
<p>استک .NET با کتابخانه‌های بالغ و مستندات کامل، بستر مناسبی برای اجرای این سرویس فراهم می‌کند.</p>
<div class="highlight">
<strong>💎 ارزش کلیدی:</strong><br>
انتخاب ابزار مناسب + پیاده‌سازی گام‌به‌گام + سخت‌سازی امنیتی + نقشه راه شفاف = سرویس احراز هویت پایدار و آماده تولید
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> در صورت تمایل به شروع پیاده‌سازی، آماده برگزاری جلسه فنی برای انتخاب نهایی ابزار (OpenIddict یا Duende)، تعیین جریان‌های OAuth 2.0 موردنیاز و تدوین برنامه اجرایی تفصیلی هستیم.
</div>
</div>
</div>
<div class="page-info">
این مستند محرمانه بوده و صرفاً جهت ارائه به کارفرمای محترم تهیه شده است. هرگونه کپی‌برداری یا افشا بدون اجازه کتبی ممنوع است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">طراحی و توسعه: هادی خزاعی اصل</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: مهر ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: محرمانه</div>
</footer>
</div>
</body>
</html>