You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Authentication is the process of verifying the identity of the user who accesses a web application. This may require the user to input a username and password to log into the web application.
12
+
</p>
13
+
<p>
14
+
Authentication is handled by the authentication middleware, which uses the registered authentication service that can have multiple authentication schemes, and each scheme is related to an authentication handler to determine later in the authorization policies which authentication handler should be used to authenticate the user and provides a <code>ClaimsIdentity</code> that represents the user in the request context.
15
+
</p>
16
+
<h3>Configuration</h3>
17
+
<p>
18
+
The authentication service is added to the application by calling the <code>addAuthentication()</code> method, and then authentication schemes can be specified by calling one of the following methods:
19
+
</p>
20
+
<ul>
21
+
<li><code>AddCookie()</code> for cookie-based authentication</li>
22
+
<li><code>AddJwtBearer()</code> for token-based authentication</li>
23
+
</ul>
24
+
<p>
25
+
The authentication middleware is used in the application by calling the <code>useAuthentication()</code> method and must be called before any middleware that depends on the user being authenticated.
26
+
</p>
27
+
<p>
28
+
This example shows the configuration of both approaches, cookie-based and token-based authentication, but you can choose one of the two approaches.
29
+
</p>
30
+
<pre><codeclass="language-php"><?php
31
+
32
+
namespace Application;
33
+
34
+
use DevNet\System\TimeSpan;
35
+
use DevNet\Web\Hosting\WebHost;
36
+
use DevNet\Web\Extensions\ApplicationBuilderExtensions;
37
+
use DevNet\Web\Extensions\ServiceCollectionExtensions;
// Adding the authentication middleware befor the endpoint middleware.
70
+
$app->useAuthentication();
71
+
$app->useEndpoint(function ($routes) {
72
+
// routes
73
+
});
74
+
});
75
+
}
76
+
}
77
+
</code></pre>
78
+
<br>
79
+
<h3>Cookie-based Authentication</h3>
80
+
<p>
81
+
The cookie-based authentication is a stateful process, which means that the server stores the user session data and sends to the client a cookie that contains a session reference, which is often stored in the browser and sent back to the server with every request to authenticate the client requests and maintain session information on the server over the stateless HTTP protocol.
82
+
</p>
83
+
<p>
84
+
The following example demonstrates the working process of cookie-based authentication using <code>ClaimsIdentity</code> and the <code>Authentication</code> service to log in and log out the user.
85
+
</p>
86
+
<pre><codeclass="language-php"><?php
87
+
88
+
use DevNet\System\Linq;
89
+
use DevNet\System\Collections\ArrayList;
90
+
use DevNet\Web\Http\HttpContext;
91
+
use DevNet\Web\Security\Authentication\AuthenticationScheme;
The token-based authentication is a stateless process. This means that the server does not store any session information about the user on its side. Instead, it sends to the client an encrypted token, typically JWT (JSON Web Token), that contains the user information and expiration time, and the client stores this token and sends it back to the server with every request, where the server does the token validation and grants access to the user.
130
+
</p>
131
+
<p>
132
+
The following example demonstrates the working process of token-based authentication using ClaimsIdentity and JwtSecurityTokenHandler to generate a JWT token and send it to the client to send it back later for authentication.
133
+
</p>
134
+
<p>
135
+
Due to the limitations of this approach, there is no option for remembering or logging out the user on the server side. However, you can easily log out by removing the token from your request header on the client side.
136
+
</p>
137
+
<pre><codeclass="language-php"><?php
138
+
139
+
use DevNet\System\Linq;
140
+
use DevNet\System\Collections\ArrayList;
141
+
use DevNet\Web\Http\HttpContext;
142
+
use DevNet\Web\Security\Authentication\AuthenticationScheme;
<b>Important:</b> The client should send back the JWT token in the Authorization header using the Bearer schema in the following format: <code>Authorization: Bearer <token></code>
0 commit comments