授权

 

1. 全局

c# api身份验证和授权

 config.Filters.Add(new AuthorizeAttribute());

 

 

2.控制器级别

   
  [Authorize]   
public class HelloController : ApiController { public string GetTest() { return "hello,world"; } }

 

3.方法级别

    public class HelloController : ApiController
    {
        [Authorize]
        public string GetTest()
        {
            return "hello,world";
        }

    }

 

访问网址会得到401的状态

http://localhost:55658/api/hello 

c# api身份验证和授权

 

如果想要在禁止全部的情况下,开放某个方法

    [Authorize]
    public class HelloController : ApiController
    {
        [AllowAnonymous]
        public string GetTest()
        {
            return "hello,world1";
        }
        public string PostTest()
        {
            return "hello,world2";
        }

    }

 

 

 身份验证

1.基本身份验证

 

2.forms身份验证

 

3.windows身份验证

 

 

 c# api身份验证和授权

 

c# api身份验证和授权

 

相关文章:

  • 2022-12-23
  • 2022-02-16
  • 2021-10-08
  • 2022-12-23
  • 2021-06-22
  • 2021-11-23
  • 2021-10-09
  • 2021-11-03
猜你喜欢
  • 2022-01-04
  • 2021-10-07
  • 2022-12-23
相关资源
相似解决方案