)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » AuthorizationHeaderHandler ()

Public general use code classes and xml files that we've compiled and used over the years:

AuthorizationHeaderHandler class of Fixed Telecommunications Network (FTN) model.

    1: using System.Collections.Generic;
    2: using System.Linq;
    3: using System.Net.Http;
    4: using System.Security.Principal;
    5: using System.Threading;
    6: using System.Threading.Tasks;
    7: using System.Web;
    8:  
    9: ////////////////////////////////////////////////////////////////////////////
   10:  
   11: /// <summary publish="true">
   12: /// AuthorizationHeaderHandler class of Fixed Telecommunications Network (FTN) model.
   13: /// </summary>
   14: /// 
   15: /// <remarks> 
   16: /// Copyright © 2006-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   17: ///
   18: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   19: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   20: ///
   21: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   22: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   23: /// 
   24: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   25: /// 
   26: /// Copyright notice: This notice may not be removed or altered from any source distribution.
   27: /// </remarks> 
   28: public class AuthorizationHeaderHandler : DelegatingHandler
   29: {
   30:     ////////////////////////////////////////////////////////////////////////////
   31:  
   32:     /// <summary>
   33:     /// 
   34:     /// </summary>
   35:     protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   36:     {
   37:         // https://www.c-sharpcorner.com/article/asp-net-mvc5-rest-web-api-authorization/
   38:  
   39:         var apiKeyHeaderKey = "ApiKey";
   40:         var userName = "apiUser";
   41:  
   42:         if (request.Headers.TryGetValues(apiKeyHeaderKey, out IEnumerable<string> apiKeyHeaderValues))// && !string.IsNullOrEmpty(authorization.Parameter))
   43:         {
   44:             var apiKeyHeaderValue = apiKeyHeaderValues.First();
   45:  
   46:             if (Ia.Ftn.Cl.Models.Business.Application.Default.ApiKeyList.Any(u => u.Key == apiKeyHeaderValue))
   47:             {
   48:                 var identity = new GenericIdentity(userName);
   49:  
   50:                 SetPrincipal(new GenericPrincipal(identity, null));
   51:             }
   52:             else
   53:             {
   54:             }
   55:         }
   56:  
   57:         return base.SendAsync(request, cancellationToken);
   58:     }
   59:  
   60:     ////////////////////////////////////////////////////////////////////////////
   61:  
   62:     /// <summary>
   63:     /// 
   64:     /// </summary>
   65:     private static void SetPrincipal(IPrincipal principal)
   66:     {
   67:         Thread.CurrentPrincipal = principal;
   68:  
   69:         //if (HttpContext.Current != null) HttpContext.Current.User = principal;
   70:     }
   71:  
   72:     ////////////////////////////////////////////////////////////////////////////
   73:     ////////////////////////////////////////////////////////////////////////////
   74: }