ASP。净心如何实现基本认证

  介绍

小编给大家分享一下ASP。净核心如何实现基本认证,希望大家阅读完这篇文章之后都有所收获、下面让我们一起去探讨吧!

<强> HTTP基本认证

在HTTP中,HTTP基本认证(基本身份验证)是一种允许网页浏览器或其他客户端程序以(用户名:口令)请求资源的身份验证方式,不要求饼,会话标识符,登录页面等标记或载体。

——所有浏览器据支持HTTP基本认证方式

——基本身证原理不保证传输凭证的安全性,仅被based64编码,并没有加密或者散列,一般部署在客户端和服务端互信的网络,在公网中应用BA认证通常与https结合https://en.wikipedia.org/wiki/Basic_access_authentication

<强>英航标准协议

英航认证协议的实施主要依靠约定的请求头/响应头,典型的浏览器和服务器的英航认证流程:

①浏览器请求应用了英航协议的网站,服务端响应一个401认证失败响应码,并写入WWW-Authenticate响应头,指示服务端支持英航协议

HTTP/1.1 401年未经授权
WWW-Authenticate:基本领域=皊ite"# WWW-Authenticate响应头包含一个领域域属性,指明HTTP基本认证的是这个资源集

或客户端在第一次请求时发送正确授权标头,从而避免被质询

②客户端based64(用户名:口令),作为授权标头值重新发送请求。

授权:基本的用户标识:密码

 ASP。净心如何实现基本认证

所以在HTTP基本认证中认证范围与领域有关(具体由服务端定义)

比;一般浏览器客户端对于www-Authenticate质询结果,会弹出口令输入窗。

<>强英航编程实践

aspnetcore网站利用FileServerMiddleware将路径映射到某文件资源,现对该文件资源访问路径应用Http英航协议。

<代码> ASP。核心网服务端实现英航认证:

①实现服务端基本认证的认证过程,质询逻辑

②实现基本身份认证交互中间件BasicAuthenticationMiddleware,要求对HttpContext使用BA.Scheme

③ASP。网络核心添加认证计划,为文件资源访问路径启用BA中间件,注意使用UseWhen插入中间件

using 系统;   using  System.Net.Http.Headers;   using  System.Security.Claims;   using 包含;   using  System.Text.Encodings.Web;   using  System.Threading.Tasks;   using  Microsoft.AspNetCore.Authentication;   using  Microsoft.Extensions.Logging;   using  Microsoft.Extensions.Options;      namespace  EqidManager.Services   {   public 才能;static  class  BasicAuthenticationScheme   {才能   ,,,public  const  string  DefaultScheme =,“Basic";   ,,}      public 才能;class  BasicAuthenticationOption: AuthenticationSchemeOptions   {才能   ,,,public  string  Realm {组,得到,,,}   ,,,public  string  UserName {组,得到,,,}   ,,,public  string  UserPwd {组,得到,,,}   ,,}      public 才能class  BasicAuthenticationHandler :, AuthenticationHandler   {才能   ,,,private  readonly  BasicAuthenticationOption  authOptions;   ,,,public  BasicAuthenticationHandler (   ,,,,,IOptionsMonitor,选项,   ,,,,,ILoggerFactory 记录器,   ,,,,,UrlEncoder 编码器,   ,,,,,ISystemClock 时钟)   ,,,,,:,基地(记录器,选项,还以为,编码器,时钟)   ,,,{   ,,,,,authOptions =, options.CurrentValue;   ,,,}      ,,,///,& lt; summary>   ,,,///,认证   ,,,///,& lt;/summary>   ,,,///,& lt; returns> & lt;/returns>   ,,,protected  override  async  Task, HandleAuthenticateAsync ()   ,,,{   ,,,,,if  (! Request.Headers.ContainsKey (“Authorization"))   ,,,,,,,return  AuthenticateResult.Fail (“Missing  Authorization  Header");   ,,,,,string 用户名,密码;   ,,,,,试一试   ,,,,,{   ,,,,,,,var  authHeader =, AuthenticationHeaderValue.Parse (Request.Headers [“Authorization"]);   ,,,,,,,var  credentialBytes =, Convert.FromBase64String (authHeader.Parameter);   ,,,,,,,var  credentials =, Encoding.UTF8.GetString (credentialBytes) .Split (& # 39;: & # 39;);   ,,,,,,,,username =,凭证[0];   ,,,,,,,,password =,凭证[1];   ,,,,,,,,var  isValidUser=, IsAuthorized(用户名、密码);   ,,,,,,,如果(isValidUser==,假)   ,,,,,,,{   ,,,,,,,,,return  AuthenticateResult.Fail (“Invalid  username 或是password");   ,,,,,,,}   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

ASP。净心如何实现基本认证