ASP. NET has no magic -- Identity, Owin, identityowin
The previous article introduces how to use ASP. in the. net mvc project, the Identity component is introduced to implement user registration, login, and Identity authentication. It is also mentioned that Identity is integrated into Owin, this chapter describes what Owin is and how to use Owin to Enhance Identity.
The main contents of this chapter include:
● What is Owin?
● About Katana
● Owin and host IIS
● Integration of Owin and Identity
● Special usage of Identity in Owin
What is Owin?
Owin (Open Web Server Interface) is an Interface standard between a. NET Web Server and a Web program. Its purpose is to decouple the application from the Server. Why decoupling? Because ASP. NET application (excluding ASP. NET Core and mono) must be deployed on the IIS server. By implementing the Owin interface, you can use a console program as the Web application host.
Owin provides an IDictionary <string, object> type named environment dictionary to store all data, including request and response data. In this dictionary, Owin defines a series of core key-value pairs:
Another important core type is the proxy for the IDictionary <string, object> (the data type analyzed above): Func <IDictionary <string, object>, task>, each delegate is an independent unit used to process requests. When multiple independent units are combined, the Owin processing pipeline is formed. In addition, the returned value is a Task, in other words, all processing units or middleware must be designed to be asynchronous to increase the system throughput.
For more information, see http://owin.org/html/owin.html.
About Katana
Since Owin is an interface standard, it must be implemented. Microsoft's implementation of Owin is a project named Katana (https://github.com/aspnet/AspNetKatana), most of the components in this project are Microsoft. as the prefix, The Owin is the component directory on github:
Katana consists of four parts: Host, server, middleware, and application. For example, the entire structure is hierarchical, from bottom to top:
● Host. The first two are relatively good examples. owinhost.exe is actually a program of the Katana project used to start the Owin application. You can run the specified Owin application through the command line (Note: All Hosts can be installed through the Nuget manager ).
● Server: a Server is a component used to receive and respond to requests.
○ In IIS, install Microsoft. Owin. Host. SystemWeb and register the HttpModule "dynamic" of Owin to the IIS processing pipeline to receive and process HTTP requests.
○ In a custom Host, install Microsoft. Owin. Host. HttpListener to explicitly open a Soket listener Based on the address and port in the form of code. OwinHost.exe also uses this component to listen for requests.
● Middleware: Middleware is essentially a delegate that implements Func <IDictionary <string, object>, Task>. It can also be created simply by inheriting the OwinMiddleware type. After the creation is complete, add the Owin's Startup type to the pipeline using the IAppBuilder type Use method.
● Application: the Application layer, Owin, and Katana do not consider a new method for Application development. In other words, they can use the previous mvc, webapi, Signalr, or static page methods to develop applications.
Owin and host IIS
The Owin in IIS Host (Microsoft. Owin. Host. SystemWeb) is actually an IHttpModule implementation:
The general method for expanding IIS request processing management using HttpModule is to add the configuration in the Web. config file. However, no configuration is added to the configuration file after Owin is introduced. Instead, the following code is used to register the configuration file when the program is running:
When using IIS as the host, Owin uses the HttpModule form for the original ASP.. net http request channel is extended, and requests are stored in ASP. the entire request is transferred to the Owin pipeline in the form of HttpModule expansion.
Integration of Owin and Identity
Identity is integrated into the processing pipeline through the Owin middleware, such as the Code:
In addition, the following configuration is added to the Web. config file when Identity is installed:
The HttpModule for Form Verification is deleted because identity verification can replace Form verification. Therefore, this function is deleted.
HttpModule list during running:
Special usage of Identity in Owin
(Note: the following code can refer to the default Project template with authentication in ASP. net mvc. Some codes change the DbContext name, namespace, and omit some configurations, such as two-factor authentication)
This is a special usage. In fact, a DbContext, UserManager, and SignInManager are created in the Owin environment dictionary for each request, this means that you can obtain these instances through Owin throughout the request process to operate on users.
1. Prepare before use to encapsulate the creation process of DbContext:
2. encapsulate the UserManager creation process (Note: UserManager creation also contains the authentication configuration for the user name and password and some authentication configuration information ):
3. encapsulate SignInManager:
The UserManager dependent on in SignInManager comes from the Owin context object.
In addition, the identity information generated through user information is encapsulated here:
4. Add the following code to the Startup File of Owin to ensure that these objects are created during the request:
Note that CreatePerOwinContext comes from the Owin extension of Identity:
The CreatePerOwinContext method adds an Owin middleware named IdentityFactoryMiddleware to the Owin pipeline, the preceding three methods insert three identical middleware (but different parameters) in the Owin pipeline. The following is the code called by IdentityFactoryMiddleware:
5. Modify the Controller code to obtain UserManager and SignInManager from the Owin Context when implementing user registration and logon:
Registration method. The Code comes from the default template (compared to the previous code that adds a judgment on the creation result ):
Logon method. The Code comes from the default template (compared to the previous code that adds a judgment on the creation result ):
6. Running effect:
7. Install the Identity localization package:
From the preceding error message, you can see the English prompt. They add the error message to the ModelState information in the Controller, and then use Html in the View. the ValidationSummary method is displayed (more about Model-related content ).
However, because the result returned by the creation user is in English, the display is also in English. To replace it with Chinese, you need to install the Chinese package: Microsoft. aspNet. identity. core. zh-Hans:
Running effect after installation:
Note: user name and password verification is configured when UserManager is created:
Summary
This article introduces Owin and ASP. net mvc, how to integrate Identity into Owin and use it. the code of the net mvc template improves the implementation of user registration and logon in the previous article.
Refer:
Http://owin.org/html/spec/owin-1.0.html#1-overview
Https://msdn.microsoft.com/en-us/library/bb470252.aspx
Https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline
Http://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin.Host.SystemWeb/PreApplicationStart.cs
Link: http://www.cnblogs.com/selimsong/p/7743112.html
ASP. NET has no magic-directory