)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
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:

    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:  
    8: ////////////////////////////////////////////////////////////////////////////
    9:  
   10: /// <summary publish="true">
   11: ///
   12: /// </summary>
   13: /// 
   14: /// <remarks> 
   15: /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   16: ///
   17: /// 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
   18: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   19: ///
   20: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   21: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   22: /// 
   23: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   24: /// 
   25: /// Copyright notice: This notice may not be removed or altered from any source distribution.
   26: /// </remarks> 
   27: public class AuthorizationHeaderHandler : DelegatingHandler
   28: {
   29:     ////////////////////////////////////////////////////////////////////////////
   30:  
   31:     /// <summary>
   32:     /// 
   33:     /// </summary>
   34:     protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   35:     {
   36:         // https://www.c-sharpcorner.com/article/asp-net-mvc5-rest-web-api-authorization/
   37:  
   38:         var apiKeyHeaderKey = "ApiKey";
   39:         var userName = "apiUser";
   40:  
   41:         if (request.Headers.TryGetValues(apiKeyHeaderKey, out IEnumerable<string> apiKeyHeaderValues))// && !string.IsNullOrEmpty(authorization.Parameter))
   42:         {
   43:             var apiKeyHeaderValue = apiKeyHeaderValues.First();
   44:  
   45:             /*
   46:             if (Ia.Ngn.Cl.Model.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:  
   58:         return base.SendAsync(request, cancellationToken);
   59:     }
   60:  
   61:     ////////////////////////////////////////////////////////////////////////////
   62:  
   63:     /// <summary>
   64:     /// 
   65:     /// </summary>
   66:     private static void SetPrincipal(IPrincipal principal)
   67:     {
   68:         Thread.CurrentPrincipal = principal;
   69:  
   70:         if (System.Web.HttpContext.Current != null) System.Web.HttpContext.Current.User = principal;
   71:     }
   72:  
   73:     ////////////////////////////////////////////////////////////////////////////
   74:     ////////////////////////////////////////////////////////////////////////////
   75: }