• WebAPI身份验证


    对WebAPI接口的开放当然要做控制,需要身份验证如何做到呢、

    shenfen1

    进行身份验证后的

    shenfen2

    服务器拒绝了访问!

    第一步添加一个CustomHandler.cs的类

       1:  using System;
       2:  using System.Collections.Generic;
       3:  using System.Linq;
       4:  using System.Web;
       5:  using System.Threading.Tasks;
       6:  using System.Net.Http;
       7:  using System.Text;
       8:  using System.Net;
       9:   
      10:  namespace MvcApplication1.Handler
      11:  {
      12:      public class customHandler : DelegatingHandler 
      13:      {
      14:          protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
      15:          {
      16:              int matchHeaderCount = request.Headers.Count((item) =>
      17:              {
      18:                  if ("key".Equals(item.Key))
      19:                  {
      20:                      foreach (var str in item.Value)
      21:                      {
      22:                          if ("11234".Equals(str))
      23:                          {
      24:                              return true;
      25:                          }
      26:                      }
      27:                  }
      28:                  return false;
      29:              });
      30:              if (matchHeaderCount > 0)
      31:              {
      32:                  return base.SendAsync(request, cancellationToken);
      33:              }
      34:              return Task.Factory.StartNew<HttpResponseMessage>(() => { return new HttpResponseMessage(HttpStatusCode.Forbidden); });
      35:          }
      36:      }
      37:  }

    第二步部署一下就ok了

       1:  using System;
       2:  using System.Collections.Generic;
       3:  using System.Linq;
       4:  using System.Web;
       5:  using System.Web.Http;
       6:  using System.Web.Mvc;
       7:  using System.Web.Optimization;
       8:  using System.Web.Routing;
       9:  using MvcApplication1.Handler;
      10:   
      11:  namespace MvcApplication1
      12:  {
      13:      // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
      14:      // 请访问 http://go.microsoft.com/?LinkId=9394801
      15:   
      16:      public class WebApiApplication : System.Web.HttpApplication
      17:      {
      18:          protected void Application_Start()
      19:          {
      20:              AreaRegistration.RegisterAllAreas();
      21:   
      22:              WebApiConfig.Register(GlobalConfiguration.Configuration);
      23:              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
      24:              RouteConfig.RegisterRoutes(RouteTable.Routes);
      25:              BundleConfig.RegisterBundles(BundleTable.Bundles);
      26:              
    GlobalConfiguration.Configuration.MessageHandlers.Add(new customHandler());
      27:          }
      28:      }
      29:  }

    添加最后一行就ok了。

    image

    客户端访问也只要在

    request的Headers添加验证信息就可以了,当然android客户端具体怎么访问还在学习中。

    
    
    学会勇敢
  • 相关阅读:
    数据库准入规则
    ubuntu go 开发环境搭建
    PHP 异步执行方式
    python 连接 hive数据库环境搭建
    Swift 发送邮件和附件
    python 爬取动态数据
    git 新建项目的一些操作
    php 爬取数据
    通过NGINX location实现一个域名访问多个项目
    Linux系统Web网站目录和文件安全权限设置
  • 原文地址:https://www.cnblogs.com/Sir-Lin/p/4746154.html
Copyright © 2020-2023  润新知