|
26 | 26 |
|
27 | 27 | ### Introduction |
28 | 28 |
|
29 | | -"One-line code" adds a JWT account and dynamic routing permission management system to "existing new or old projects." |
| 29 | +"One-line code" adds identity management web for your new/old projects |
30 | 30 |
|
31 | 31 | <table> |
32 | 32 | <tr> |
|
42 | 42 |
|
43 | 43 | ### Features |
44 | 44 |
|
45 | | -- Simple: Out of the box for SPA, SSR, API, MVC, and Razor Page. |
46 | | -- Multi-platform: Supports Linux and macOS. |
47 | | -- Dynamic: Runtime dynamic endpoint permission management . |
48 | | -- Incremental: Configuration based on business needs. |
49 | | -- Compatible: Doesn't require intrusive modifications to existing systems and can be used with other permission frameworks. |
50 | | -- Supports multiple databases. |
| 45 | +- Compatibility: Based JWT, Cookie, and Session that follow the .NET Identity standard. |
| 46 | +- Out of the box: Easy integration, MiniAuth works with APIs, MVC, Razor Pages. |
| 47 | +- Multi-platform: Supports Linux, macOS environments. |
| 48 | +- Multiple Database Support: Compatible with any database that follow the Identity EF Core standard. |
51 | 49 |
|
52 | 50 | ### Installation |
53 | 51 |
|
54 | | -Install the package from [NuGet](https://www.nuget.org/packages/MiniAuth). |
| 52 | +Install the package from [NuGet](https://www.nuget.org/packages/MiniAuth): |
55 | 53 |
|
56 | | -### Quick Start - Video |
57 | | - |
58 | | -[Video Link](https://www.youtube.com/watch?v=MBaDJVZI-ik) |
| 54 | +``` |
| 55 | +dotnet add package MiniAuth |
| 56 | +// or |
| 57 | +NuGet\Install-Package MiniAuth |
| 58 | +``` |
59 | 59 |
|
60 | 60 | ### Quick Start |
61 | 61 |
|
62 | | -Add one line of code to Startup and run the project: |
| 62 | +Add a single line of code `services.AddMiniAuth()` in Startup, then run your project. Example: |
63 | 63 |
|
64 | 64 | ```csharp |
65 | | -app.UseMiniAuth(); |
66 | | -``` |
67 | | - |
68 | | -The default admin account "miniauth" and password "miniauth" `(remember to change the password)`. |
69 | | -The admin page: `http(s)://yourhost/miniauth/index.html`. |
| 65 | +public class Program |
| 66 | +{ |
| 67 | + public static void Main(string[] args) |
| 68 | + { |
| 69 | + var builder = WebApplication.CreateBuilder(args); |
70 | 70 |
|
71 | | -Note : Please put UseMiniAuth after route creating for system get endpoint data, e.g. |
| 71 | + builder.Services.AddMiniAuth(); |
72 | 72 |
|
73 | | -```c# |
74 | | -app.UseRouting(); |
75 | | -app.UseMiniAuth(); |
| 73 | + var app = builder.Build(); |
| 74 | + app.Run(); |
| 75 | + } |
| 76 | +} |
76 | 77 | ``` |
77 | 78 |
|
78 | | -#### Login |
| 79 | +The default admin account is `[email protected]` with the password `E7c4f679-f379-42bf-b547-684d456bc37f` (remember to change the password). The admin page can be accessed at `http(s)://yourhost/miniauth/index.html`. |
| 80 | + |
| 81 | +Note: If you already have your own identity auth, please follow the instructions below. |
79 | 82 |
|
80 | | -Use the API endpoint `Post /MiniAuth/login` and pass the JSON body: |
| 83 | +### Existing Identity Setup |
81 | 84 |
|
82 | | -```json |
| 85 | +Turn off autoUse for `AddMiniAuth`, and replace it with your own IdentityDBContext, user, and permission authentication in UseMiniAuth, placing it after your own Auth. Example: |
| 86 | + |
| 87 | +```csharp |
| 88 | +public static void Main(string[] args) |
83 | 89 | { |
84 | | - "username":"username", |
85 | | - "password":"password" |
86 | | -} |
87 | | -``` |
88 | | -You can retrieve the JWT Token with the key `X-MiniAuth-Token` from the Headers or Response Body. |
89 | | -By default, the same domain will automatically add token cookie. |
| 90 | + var builder = WebApplication.CreateBuilder(args); |
90 | 91 |
|
91 | | -#### Logout |
| 92 | + var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); |
| 93 | + builder.Services.AddDbContext<ApplicationDbContext>(options => |
| 94 | + options.UseSqlServer(connectionString)); |
| 95 | + builder.Services.AddDatabaseDeveloperPageExceptionFilter(); |
92 | 96 |
|
93 | | -Delete the `X-MiniAuth-Token` cookie to log out of the system. |
94 | | -You can also use the API endpoint `Get /MiniAuth/logout` to delete the cookie and redirect to the login page. |
| 97 | + builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) |
| 98 | + .AddRoles<IdentityRole>() |
| 99 | + .AddEntityFrameworkStores<ApplicationDbContext>(); |
95 | 100 |
|
96 | | -#### Get Current User Data |
| 101 | + builder.Services.AddControllersWithViews(); |
97 | 102 |
|
98 | | -Note: Read JWT Token user data from the Request, not from the DB. |
| 103 | + builder.Services.AddMiniAuth(autoUse: false); // <= ❗❗❗ |
99 | 104 |
|
100 | | -```C# |
101 | | -public class YourController : Controller |
102 | | -{ |
103 | | - public ActionResult UserInfo() |
104 | | - { |
105 | | - var user = this.GetMiniAuthUser(); |
106 | | - //... |
107 | | - } |
| 105 | + var app = builder.Build(); |
| 106 | + |
| 107 | + app.UseMiniAuth<ApplicationDbContext, IdentityUser, IdentityRole>(); // <= ❗❗❗ |
| 108 | + app.MapControllerRoute( |
| 109 | + name: "default", |
| 110 | + pattern: "{controller=Home}/{action=Index}/{id?}"); |
| 111 | + app.MapRazorPages(); |
| 112 | + |
| 113 | + app.Run(); |
108 | 114 | } |
109 | 115 | ``` |
110 | 116 |
|
111 | | -### Changing the Database |
112 | | - |
113 | | -#### SQLite |
114 | | - |
115 | | -SQLite is used by default, no additional configuration required. |
116 | | - |
117 | | -#### SQL Server |
118 | | - |
119 | | -Currently supports `SQL Server 2012 (version 11.x) and higher`. |
120 | | -Run the following script based on your environment: |
121 | | - |
122 | | -```sql |
123 | | -create database miniauth; /*Following your env to change sql*/ |
124 | | - |
125 | | -create table miniauth..users ( |
126 | | - id nvarchar(20) not null primary key, |
127 | | - username nvarchar(20) not null unique, |
128 | | - password nvarchar(100) not null, |
129 | | - roles nvarchar(2000), |
130 | | - enable int default 1, |
131 | | - first_name nvarchar(200), |
132 | | - last_name nvarchar(200), |
133 | | - mail nvarchar(200), |
134 | | - emp_no nvarchar(50) , |
135 | | - type nvarchar(20) |
136 | | -); |
137 | | - |
138 | | -create table miniauth..roles ( |
139 | | - id nvarchar(20) primary key, |
140 | | - name nvarchar(200) not null unique, |
141 | | - enable int default (1) not null, |
142 | | - type nvarchar(20) |
143 | | -); |
144 | | - |
145 | | -create table miniauth..endpoints ( |
146 | | - id nvarchar(400) primary key, |
147 | | - type nvarchar(20) not null, |
148 | | - name nvarchar(400) not null, |
149 | | - route nvarchar(400) not null, |
150 | | - methods nvarchar(80), |
151 | | - enable int default (1) not null, |
152 | | - redirecttologinpage int not null, |
153 | | - roles nvarchar(2000) |
154 | | -); |
155 | | - |
156 | | --- hashed password will update on first run time |
157 | | -insert into miniauth..roles (id,type,name) values ('13414618672271360','miniauth','miniauth-admin'); |
158 | | -insert into miniauth..users (id,type,username,password,roles) values ('13414618672271350','miniauth','miniauth','','13414618672271360'); |
159 | | -``` |
160 | | -In Startup, add the injection code: |
| 117 | +#### Order Matters |
| 118 | +Please place UseMiniAuth after routing generation; otherwise, the system won't be able to obtain routing data for permission checks, like: |
161 | 119 |
|
162 | 120 | ```csharp |
163 | | -builder.Services.AddSingleton<IMiniAuthDB>( |
164 | | - new MiniAuthDB<System.Data.SqlClient.SqlConnection>("Data Source=(localdb)\\MSSQLLocalDB;Integrated Security=SSPI;Initial Catalog=miniauth;app=MiniAuth") |
165 | | -); |
| 121 | +app.UseRouting(); |
| 122 | +app.UseMiniAuth(); |
166 | 123 | ``` |
167 | 124 |
|
168 | | -### Settings and Options |
| 125 | +#### Note: Adding Role Rules |
169 | 126 |
|
170 | | -#### Default Mode |
171 | | -- MiniAuth's default mode is centralized user management by IT Admin, requiring an Admin account for operations like user registration and password reset. |
172 | | -- Initially, all existing endpoints are added to the system's permission management |
173 | | -- New endpoints are automatically detected and added during system restarts. |
174 | | -- User passwords reset by the reset button to generate a random password. |
175 | | -- Remember to reset the password after creating a user. |
| 127 | +Please add `AddRoles<IdentityRole>()`; otherwise `[Authorize(Roles = "YourRole")]` won't work. |
| 128 | +```C# |
| 129 | +builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) |
| 130 | + .AddRoles<IdentityRole>() // ❗❗❗ |
| 131 | + .AddEntityFrameworkStores<ApplicationDbContext>(); |
| 132 | +``` |
176 | 133 |
|
177 | | -#### Login and User Authentication |
178 | | -Non-ApiController defaults to redirecting to the login.html page for login. |
179 | | -ApiController-based controllers default to returning a 401 status code. |
| 134 | +### Changing Databases |
180 | 135 |
|
181 | | -#### Default Expiration Time |
182 | | -`MiniAuthOptions.ExpirationMinuteTime` has a default expiration time of 7 days. You can change like following code (note the unit is `minutes`): |
| 136 | +MiniAuth system defaults to using SQLite without any code settings required. If you need to switch, specify a different database type in `app.UseMiniAuth`. |
183 | 137 |
|
184 | | -```C# |
185 | | -services.AddSingleton<MiniAuthOptions>(new MiniAuthOptions { ExpirationMinuteTime = 12 * 24 * 60 }); |
186 | | -``` |
| 138 | +### Configuration and Options |
| 139 | + |
| 140 | +#### Default Mode |
| 141 | + |
| 142 | +- MiniAuth operates in a default mode where IT Admin centrally manages user operations like registration and password reset, requiring an Admin privilege account to perform these actions. Default Role = miniauth-admin. |
187 | 143 |
|
188 | | -#### Custom Login - js, css |
189 | | -Add `app.UseStaticFiles()` before `UseMiniAuth` and create `wwwroot\MiniAuth\login.css` and `wwwroot\MiniAuth\login.js` for customization. |
| 144 | +#### Login and User Validation |
190 | 145 |
|
191 | | -### Security |
192 | | -#### Encryption and Keys |
193 | | -The default JWT handling method is `RS256 + X509`. During the first run, new certificates (`miniauth.pfx` and `miniauthsalt.cer`) are generated locally. Please manage these securely. |
| 146 | +For non-ApiController, login redirects to the login.html page (determined by Headers["X-Requested-With"] == "XMLHttpRequest" or ApiControllerAttribute). |
| 147 | +ApiController Controllers do not redirect to the login page by default but return a 401 status code. |
194 | 148 |
|
195 | 149 | ### Distributed Systems |
196 | | -- For distributed systems, use databases like SQL Server, MySQL, or PostgreSQL instead of the default SQLite. |
197 | | -- Ensure that `miniauth.pfx` and `miniauthsalt.cer` are the same across all machines; |
| 150 | + |
| 151 | +- Please switch the database source to SQL Server, MySQL, PostgreSQL, etc. |
198 | 152 |
|
199 | 153 | ### Release Notes |
200 | | -Please refer to the [Release Notes](releases) for update details. |
| 154 | + |
| 155 | +Refer to the [Release Notes](releases) for updates. |
| 156 | + |
| 157 | +### TODO |
| 158 | + |
| 159 | +Link: [MiniAuth.Identify project](https://github.com/orgs/mini-software/projects/7/views/1) |
0 commit comments