一個基於 Role-Based 權限管理的現代化線上差勤管理系統,支援多層級使用者權限、簽到簽退管理、假單申請與簽核流程。
| 角色名稱 | 權限等級 | 說明 |
|---|---|---|
| Admin | 9 | 系統最高權限,管理帳號與權限設定 |
| HR | 8 | 可維護部門與職級 |
| Director | 7 | 可審核部屬假單 |
| Manager | 6 | 中階主管 |
| Section Manager | 5 | 部門主管 |
| Specialist | 4 | 資深員工 |
| Clerk | 3 | 行政助理或基礎人員 |
| Operator | 2 | 作業員 |
| User | 0 | 一般使用者(註冊預設) |
Users - 使用者基本資料UserRoles - 使用者角色關聯Departments - 部門資訊JobLevels - 職級定義Attendances - 出勤記錄LeaveRequests - 假單申請Notifications - 系統通知好的,我們可以先從 C# ASP.NET Core Web API 的部分著手開發,依照規劃書內容,我建議先完成以下幾個基礎模組,搭建整體架構與資料模型:
建立 ASP.NET Core 9 Web API 專案
安裝 NuGet 套件(例如:
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.AspNetCore.Authentication.JwtBearer
Swashbuckle.AspNetCore(Swagger)
AutoMapper
設定 appsettings.json(連線字串、JWT 密鑰等)
User
UserRoles
Departments
JobLevels
Attendances
LeaveRequests
Notifications
我會帶你建立一個 ASP.NET Core 9 Web API 專案骨架,包含:
ASP.NET Core Identity
JWT 驗證機制
EF Core(使用 SQLite)
Swagger(API 文件)
基本資料夾結構與配置
✅ 第一步:建立 Web API 專案
打開終端機/命令提示字元,執行:
dotnet new webapi -n AttendanceSystem.API
cd AttendanceSystem.API
✅ 第二步:安裝必要的 NuGet 套件
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer
dotnet add package Swashbuckle.AspNetCore
dotnet add package Microsoft.EntityFrameworkCore.Design
✅ 第三步:建立資料夾結構(可手動或指令建立)
AttendanceSystem.API/
├── Controllers/
├── Data/
├── Models/
├── Services/
├── DTOs/
├── Helpers/
└── Program.cs / appsettings.json
✅ 第四步:建立 User 模型與資料庫上下文
🔹 Models/User.cs
using Microsoft.AspNetCore.Identity;
namespace AttendanceSystem.API.Models
{
public class User : IdentityUser
{
public string FullName { get; set; }
public int DepartmentId { get; set; }
public int JobLevelId { get; set; }
public Department Department { get; set; }
public JobLevel JobLevel { get; set; }
}
}
🔹 Models/Department.cs
namespace AttendanceSystem.API.Models
{
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<User> Users { get; set; }
}
}
🔹 Models/JobLevel.cs
namespace AttendanceSystem.API.Models
{
public class JobLevel
{
public int Id { get; set; }
public string Title { get; set; }
public int Level { get; set; }
public ICollection<User> Users { get; set; }
}
}
🔹 Data/AppDbContext.cs
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using AttendanceSystem.API.Models;
namespace AttendanceSystem.API.Data
{
public class AppDbContext : IdentityDbContext<User>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<Department> Departments { get; set; }
public DbSet<JobLevel> JobLevels { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Department>().HasMany(d => d.Users).WithOne(u => u.Department).HasForeignKey(u => u.DepartmentId);
builder.Entity<JobLevel>().HasMany(j => j.Users).WithOne(u => u.JobLevel).HasForeignKey(u => u.JobLevelId);
}
}
}
✅ 第五步:配置資料庫與 Identity(在 Program.cs)
修改 Program.cs 加入以下:
using AttendanceSystem.API.Data;
using AttendanceSystem.API.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// 設定資料庫
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString(“DefaultConnection”)));
// 設定 Identity
builder.Services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
// JWT 設定
var jwtKey = builder.Configuration[“Jwt:Key”];
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)),
ValidateIssuer = false,
ValidateAudience = false
};
});
// Swagger with JWT Support
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc(“v1”, new OpenApiInfo { Title = “AttendanceSystem API”, Version = “v1” });
// JWT 驗證機制說明
var jwtSecurityScheme = new OpenApiSecurityScheme
{
BearerFormat = "JWT",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer",
Description = "Put **_ONLY_** your JWT Bearer token on textbox below!",
Reference = new OpenApiReference
{
Id = JwtBearerDefaults.AuthenticationScheme,
Type = ReferenceType.SecurityScheme
}
};
c.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme);
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{ jwtSecurityScheme, Array.Empty<string>() }
});
});
builder.Services.AddControllers();
var app = builder.Build();
// 啟用中介軟體
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
✅ 第六步:設定 appsettings.json
{
“ConnectionStrings”: {
“DefaultConnection”: “Data Source=attendance.db”
},
“Jwt”: {
“Key”: “your_super_secret_key_here”
},
“Logging”: {
“LogLevel”: {
“Default”: “Information”,
“Microsoft.AspNetCore”: “Warning”
}
},
“AllowedHosts”: “*”
}
✅ 第七步:建立資料庫
在專案目錄執行:
dotnet ef migrations add Init
dotnet ef database update
✅ 下一步要做什麼?
現在專案骨架已經搭好,你可以選擇以下其中一個繼續:
🔐 建立 JWT 註冊/登入功能與 Token 回傳 API
🧾 實作出勤功能(簽到/簽退)
🧑💼 角色管理(設定 Admin、Manager、User)
📦 我幫你打包目前程式碼範本(可直接複製使用)
你想先完成哪一部分?或者要我幫你生成完整的 starter repo zip?