如何在ASP.Net Web API中为特定控制器启用Windows身份验证 [英] How to enable Windows authentication for specific controller in ASP.Net Web API

查看:164
本文介绍了如何在ASP.Net Web API中为特定控制器启用Windows身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有一种方法只能为特定ASP.Net Web API控制器的特定操作启用Windows身份验证,那么我就会徘徊。我的Web API Web服务有几个具有大量操作的控制器,但只有一个控制器的一个操作需要Windows身份验证。此Web服务使用Web API 2.1实现,并托管在IIS(v7.5及更高版本)中。尽管如此,它是一个Intranet Web服务,我不想在控制器和不需要它的操作上启用Windows身份验证。如果有办法为特定控制器和操作启用Windows身份验证,请告诉我。

I was wandering if there is a way to enable Windows authentication only for the particular action of the particular ASP.Net Web API controller. My Web API web service has a couple of controllers with numerous actions, but only one action of one controller needs Windows authentication. This web services is implemented using Web API 2.1, and is hosted in IIS (v7.5 and above). Even though, it’s an intranet web service I don’t want to enable windows authentication on controllers and actions that don’t need it. Please let me know if there is a way to enable Windows authentication for a specific controller and action.

我的Web服务代码与下面的代码类似。只有 Controller1.Action1 实施的端点 api / controller1 / action1 才需要进行Windows身份验证。其余操作不需要Windows身份验证:

My web service code is similar to the code below. Only endpoint api/controller1/action1 implemented by Controller1.Action1 requires windows authentication. The rest of actions don't need Windows authentication:

[RoutePrefix("api/controller1")]
public class Controller1: ApiController
{
    [Route("action1")]
    public HttpResponseMessage Action1()
    {
        return Request.CreateResponse<object>(HttpStatusCode.OK, null);
    }
    [Route("action2")]
    public HttpResponseMessage Action2()
    {
        return Request.CreateResponse<object>(HttpStatusCode.OK, null);
    }
}


[RoutePrefix("api/controller2")]
public class Controller2 : ApiController
{
    [Route("action1")]
    public HttpResponseMessage Action1()
    {
        return Request.CreateResponse<object>(HttpStatusCode.OK, null);
    }
    [Route("action2")]
    public HttpResponseMessage Action2()
    {
        return Request.CreateResponse<object>(HttpStatusCode.OK, null);
    }
}

谢谢你,
Rita

Thank you, Rita

推荐答案

这是你想要的吗?将其添加到您的配置文件中。

is this what your want? adding this to your config file.

<location path="api/controller1">
  <system.web>
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
</location>

这篇关于如何在ASP.Net Web API中为特定控制器启用Windows身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆