Asp. NET no magic--identity and Owin

Source: Internet
Author: User

The previous article describes how to introduce identity components in an ASP. NET MVC project for user registration, login, and authentication, and also mentions that identity is integrated into Owin. This chapter describes what Owin is and how to use Owin to enhance the identity.

The main contents of this chapter are:

What is Owin
About Katana
Owin with the host IIS
Integration of Owin with identity
The special usage of identity in Owin

What is Owin

Owin (Open Web server Interface) It is an interface standard between a. NET Web server and a Web application that is designed to decouple applications from the server. Why do I need decoupling? Because ASP. NET applications (which do not contain ASP. NET core and mono) need to be deployed on the IIS server, a console program can be hosted as a Web application by implementing the Owin interface.

Owin essentially provides a idictionary<string, called an environment dictionary, object> type 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 a proxy with a parameter of idictionary<string, object> (that is, the type used to store data above): Func<idictionary<string, Object Task> each delegate is a separate unit that is used to process the request, and the collection of independent units together forms the owin processing pipeline . In addition, the return value is a task, in other words all processing units or middleware need to be designed to be asynchronous, can improve the system's throughput.
For more information, refer to the documentation: http://owin.org/html/owin.html

About Katana

Owin since it is an interface standard, then there must be implementation, Microsoft's implementation of Owin is a project called Katana (Https://github.com/aspnet/AspNetKatana), Most of the components in this project are prefixed with Microsoft.owin, which is part of the component directory on GitHub:

  

Katana mainly consists of 4 parts, namely, host, server, middleware and application, such as the whole structure is hierarchical, from bottom to top:

  

Host: Host, the host with Katana optional Owin has IIS, Custom hosts (such as the console program) , and OwinHost.exe. The previous two is a good understanding, for OwinHost.exe is actually a katana project to start the Owin application program, the command line can be run by the specified Owin application (Note: All hosts can be installed through the NuGet manager).

  


Server: The component that is used to receive and respond to requests.
0 under IIS, the Owin httpmodule "Dynamic" is registered in the IIS processing pipeline to receive processing HTTP requests by installing MICROSOFT.OWIN.HOST.SYSTEMWEB.
0 in a custom host, by installing Microsoft.Owin.Host.HttpListener, explicitly open a soket based on the address and port in code to listen for requests. OwinHost.exe also uses this component to listen for requests.
Middleware: Middleware, in essence, is an implementation of func<idictionary<string, Object>, task> of the delegate, you can simply inherit the Owinmiddleware type to create. After creation is completed, it is added to the pipeline by the Iappbuilder type use method in the startup type of Owin.
Application: The application layer, Owin, and Katana do not consider a new way to develop the application, in other words, you can use the previous MVC, WEBAPI, signalr, or static pages and other ways to develop the application.

Owin with the host IIS

Owin in the IIS host (MICROSOFT.OWIN.HOST.SYSTEMWEB) is actually a IHttpModule implementation:

  

The general approach to using HttpModule to extend the request handling management of IIS is to add it in the Web. config file using a configuration method. However, the introduction of Owin does not include any configuration in the configuration file, but is registered with the following code when the program is run:

  

Owin, when using IIS as a host, extends the original ASP. NET HTTP request channel through HttpModule, requesting that the entire request be transferred to the Owin pipeline processing through the HttpModule extension in the pipeline process of ASP.

Integration of Owin with identity

Identity is integrated into the processing pipeline by means of Owin middleware, such as code:

  

In addition, the following configuration is included in the Web. config file while installing identity:

  

Delete the form verification of the HttpModule, the reason is also the identity of authentication can replace the form verification, so the function was deleted.

List of HttpModule at run time:

  

The special usage of identity in Owin

(Note: The following code can refer to the default Authenticated project template for ASP. Some of the code has changed DbContext name, namespace, and omitted partial configuration, such as two-factor validation, etc.)

Said to be a special use, in fact, every time a request is created a dbcontext, Usermanager, and SigninmanageR in the Owin Environment dictionary , This means that the entire request process can be owin to get these instances to operate on the user.

1. Pre-use preparation, package DbContext creation process:

  

2. Encapsulating the Usermanager creation process (Note: Usermanager's creation also contains a validation configuration for the user name, password, and some authentication configuration information):

  

3. Package Signinmanager:

  

The usermanager that are dependent on the Signinmanager are from the Owin context object.

In addition, the user information generated by the identity information is encapsulated:

  

4. Add the following code to the Owin startup file to ensure that the objects are created when requested:

  

It should be noted that createperowincontext from the identity of the Owin expansion:

  

The Createperowincontext method actually adds a owin middleware called Identityfactorymiddleware for the Owin pipeline, meaning that the above three methods insert three identical middleware in the Owin pipeline ( But the parameters are different), the following is the code for the Identityfactorymiddleware call:

  

5. Modify the controller code, in the implementation of user registration, login function, from the context of Owin to obtain Usermanager and Signinmanager to achieve:

Registration method, code from the default template (compared to the previous code added to the creation of the results of the judgment):

  

Login method, code from the default template (in contrast to the previous code added to the creation of the results of the judgment):

  

6. Operating Effect:

  

  

7. Install the identity of the Chinese package:

You can see the English prompts from the above error prompts by adding an error message to the Modelstate information in the controller and then displaying it through the Html.validationsummary method in the view ( More about model-related content).

However, since the creation of the user returned results information is in English, so the display is also in English, if you want to replace it with Chinese, then need to install the Chinese package: Microsoft.aspnet.identity.core.zh-hans:

  

Installation finishes running effect:

  

Note: The authentication of username and password is configured when Usermanager is created:

  

Summary

This article describes how Owin and ASP. NET MVC integrates identity into Owin and uses the code from the ASP. NET MVC template to improve the implementation of user registration and login in previous articles.

Reference:

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

This article link: http://www.cnblogs.com/selimsong/p/7743112.html

Asp. NET no magic--Directory

Asp. NET no magic--identity and Owin

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.