"One-line code" adds identity management web for your new/old projects
Install the package from NuGet:
dotnet add package MiniAuth
// or
NuGet\Install-Package MiniAuth
Add a single line of code services.AddMiniAuth()
in Startup, then run your project. Example:
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMiniAuth();
var app = builder.Build();
app.Run();
}
}
The default admin account is admin@mini-software.github.io
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
.
Note: If you already have your own identity auth, please follow the instructions below.
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:
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
builder.Services.AddMiniAuth(autoUse: false); // <= ❗❗❗
var app = builder.Build();
app.UseMiniAuth<ApplicationDbContext, IdentityUser, IdentityRole>(); // <= ❗❗❗
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
}
Please place UseMiniAuth after routing generation; otherwise, the system won't be able to obtain routing data for permission checks, like:
app.UseRouting();
app.UseMiniAuth();
Please add AddRoles<IdentityRole>()
; otherwise [Authorize(Roles = "YourRole")]
won't work.
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>() // ❗❗❗
.AddEntityFrameworkStores<ApplicationDbContext>();
If you only want to use your own login logic, page, and API, you can custom the login path and turn off the switch.
// before add service
MiniAuthOptions.LoginPath = "/Identity/Account/Login";
MiniAuthOptions.DisableMiniAuthLogin = true;
MiniAuth system defaults to using SQLite without any code settings required. If you need to switch, specify a different database type in app.UseMiniAuth
.
For non-ApiController, login redirects to the login.html page (determined by Headers["X-Requested-With"] == "XMLHttpRequest" or ApiControllerAttribute). ApiController Controllers do not redirect to the login page by default but return a 401 status code.
/src/Frontend_Identity
. You can update the miniauth UI after using npm run build
.Refer to the Release Notes for updates.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。