Preface
In many cases we don’t actually need the complex user system that asp.net core comes with, based on roles, various concepts, and You have to use EF Core, and in web applications, information is stored in cookie for communication (I don’t like to put it in cookies, because once I ran the web application in the safari browser on the mac system , when cross-domain cookies cannot be set, I have to use a very special method, remember it is iframe, which is quite troublesome, so I still like to put it in the custom header ), I feel kidnapped by Microsoft after using it. However, this is completely a personal preference. You can do whatever you like. I have provided another way here so that you can have one more choice.
I use asp.net core's Dependency Injection to define a set of user authentication and authorization for my own system. You can refer to this to define your own. Limited to user system.
Aspect-orientedProgramming(AOP)
In my opinion, Middleware and Filter are both aspects in asp.net core. We can put authentication and authorization in these two places. I personally prefer to put the authentication in Middleware, which can intercept and return illegal attacks early.
Dependency Injection (DI)
There are three types of dependency injectionLife cycle
1. From the initiation to the end of the same request. (services.AddScoped)
2. Each time it is injected, it is newly created. (services.AddTransient)
3. Singleton, from the beginning of the application to the end of the application. (services.AddSingleton)
My custom user class uses services.AddScoped.
Specific methods
1. Define user class
1 // 用户类,随便写的2 public class MyUser3 {4 public string Token { get; set; }5 public string UserName { get; set; }6 }
2. Register user class
in Startup.cs The ConfigureServices function:
1 // This method gets called by the runtime. Use this method to add services to the container.2 public void ConfigureServices(IServiceCollection services)3 {4 ...5 // 注册自定义用户类6 services.AddScoped(typeof(MyUser));7 ...8 }
custom user class is registered through services.AddScoped because I want it to be in the same request , Middleware, filter, controllerreference refers to the same object.
3. Inject into Middleware
1 // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project 2 public class AuthenticationMiddleware 3 { 4 private readonly RequestDelegate _next; 5 private IOptions<headerconfig> _optionsAccessor; 6 7 public AuthenticationMiddleware(RequestDelegate next, IOptions<headerconfig> optionsAccessor) 8 { 9 _next = next;10 _optionsAccessor = optionsAccessor;11 }12 13 public async Task Invoke(HttpContext httpContext, MyUser user)14 {15 var token = httpContext.Request.Headers[_optionsAccessor.Value.AuthHeader].FirstOrDefault();16 if (!IsValidate(token))17 {18 httpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;19 httpContext.Response.ContentType = "text/plain";20 await httpContext.Response.WriteAsync("UnAuthentication");21 }22 else23 {24 // 设置用户的token25 user.Token = token;26 await _next(httpContext);27 }28 }29 30 // 随便写的,大家可以加入些加密,解密的来判断合法性,大家自由发挥31 private bool IsValidate(string token)32 {33 return !string.IsNullOrEmpty(token);34 }35 }36 37 // Extension method used to add the middleware to the HTTP request pipeline.38 public static class AuthenticationMiddlewareExtensions39 {40 public static IApplicationBuilder UseAuthenticationMiddleware(this IApplicationBuilder builder)41 {42 return builder.UseMiddleware<authenticationmiddleware>();43 }44 }</authenticationmiddleware></headerconfig></headerconfig>
I found that if I want to inject the interface/class into Middleware in Scoped mode, just The class/interface to be injected needs to be placed in the parameters of the Invoke function, not the constructor# of Middleware ##, I guess this is why Middleware does not inherit the base class or interface, and defines Invoke in the base class or interface. If it defines Invoke in the base class or interface, this Invoke will inevitably The parameters must be fixed, so dependency injection is difficult.
4. Only by configuring certain paths will the Middleware be used.1 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 2 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 3 { 4 loggerFactory.AddConsole(Configuration.GetSection("Logging")); 5 loggerFactory.AddDebug(); 6 // Set up nlog 7 loggerFactory.AddNLog(); 8 app.AddNLogWeb(); 9 10 // 除了特殊路径外,都需要加上认证的Middleware11 app.MapWhen(context => !context.Request.Path.StartsWithSegments("/api/token")12 && !context.Request.Path.StartsWithSegments("/swagger"), x =>13 {14 // 使用自定义的Middleware15 x.UseAuthenticationMiddleware();16 // 使用通用的Middleware17 ConfigCommonMiddleware(x);18 });19 // 使用通用的Middleware20 ConfigCommonMiddleware(app);21 22 // Enable middleware to serve generated Swagger as a JSON endpoint.23 app.UseSwagger();24 25 // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint.26 app.UseSwaggerUI(c =>27 {28 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");29 });30 }31 32 // 配置通用的Middleware33 private void ConfigCommonMiddleware(IApplicationBuilder app)34 {35 // cors36 app.UseCors("AllowAll");37 38 app.UseExceptionMiddleware();39 // app.UseLogRequestMiddleware();40 app.UseMvc();41 }No authentication is required for obtaining tokens and viewing api documents. 5. Inject into Filter
1 public class NeedAuthAttribute : ActionFilterAttribute 2 { 3 private string _name = string.Empty; 4 private MyUser _user; 5 6 public NeedAuthAttribute(MyUser user, string name = "") 7 { 8 _name = name; 9 _user = user;10 }11 12 public override void OnActionExecuting(ActionExecutingContext context)13 {14 this._user.UserName = "aaa";15 }16 }Here I created a class with Filter can also inject database access classes, so that we can obtain the corresponding user information through token in the database. 6. Using Filter
1 [TypeFilter(typeof(NeedAuthAttribute), Arguments = new object[]{ "bbb" }, Order = 1)]2 public class ValuesController : ControllerTypeFilter is used here to load the Filter using dependency injection, and you can set parameters and the order of the Filter. The default Filter order is Global Settings->Controller->Action, and Order is 0 by default. We can change this order by setting Order. 7. Inject into the Controller
1 public class ValuesController : Controller 2 { 3 private MyUser _user; 4 5 public ValuesController(MyUser user) 6 { 7 _user = user; 8 } 9 ...10 }Inject into the constructor of the Controller, so that we can use our customized user in the Action of the Controller , you can know which user is currently calling this Action.
The above is the detailed content of asp.net core uses DI to implement a customized user system. For more information, please follow other related articles on the PHP Chinese website!

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),