博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Authentication and Authorization in ASP.NET Web API
阅读量:6969 次
发布时间:2019-06-27

本文共 7887 字,大约阅读时间需要 26 分钟。

 

You've created a web API, but now you want to control access to it. In this series of articles, we'll look at some options for securing a web API from unauthorized users. This series will cover both authentication and authorization.

  • Authentication is knowing the identity of the user. For example, Alice logs in with her username and password, and the server uses the password to authenticate Alice.
  • Authorization is deciding whether a user is allowed to perform an action. For example, Alice has permission to get a resource but not create a resource.

    1

The first article in the series gives a general overview of authentication and authorization in ASP.NET Web API. Other topics describe common authentication scenarios for Web API.

Note

Thanks to the people who reviewed this series and provided valuable feedback: Rick Anderson, Levi Broderick, Barry Dorrans, Tom Dykstra, Hongmei Ge, David Matson, Daniel Roth, Tim Teebken.

Authentication

Web API assumes that authentication happens in the host. For web-hosting, the host is IIS, which uses HTTP modules for authentication. You can configure your project to use any of the authentication modules built in to IIS or ASP.NET, or write your own HTTP module to perform custom authentication.1

When the host authenticates the user, it creates a principal, which is an  object that represents the security context under which code is running. The host attaches the principal to the current thread by setting Thread.CurrentPrincipal. The principal contains an associated Identityobject that contains information about the user. If the user is authenticated, the Identity.IsAuthenticated property returns true. For anonymous requests, IsAuthenticated returns false. For more information about principals, see .

HTTP Message Handlers for Authentication

Instead of using the host for authentication, you can put authentication logic into an . In that case, the message handler examines the HTTP request and sets the principal.

When should you use message handlers for authentication? Here are some tradeoffs:

  • An HTTP module sees all requests that go through the ASP.NET pipeline. A message handler only sees requests that are routed to Web API.
  • You can set per-route message handlers, which lets you apply an authentication scheme to a specific route.
  • HTTP modules are specific to IIS. Message handlers are host-agnostic, so they can be used with both web-hosting and self-hosting.
  • HTTP modules participate in IIS logging, auditing, and so on.
  • HTTP modules run earlier in the pipeline. If you handle authentication in a message handler, the principal does not get set until the handler runs. Moreover, the principal reverts back to the previous principal when the response leaves the message handler.

Generally, if you don't need to support self-hosting, an HTTP module is a better option. If you need to support self-hosting, consider a message handler.

Setting the Principal

If your application performs any custom authentication logic, you must set the principal on two places:

  • Thread.CurrentPrincipal. This property is the standard way to set the thread's principal in .NET.
  • HttpContext.Current.User. This property is specific to ASP.NET.

The following code shows how to set the principal:

C#Copy

private						void								SetPrincipal(IPrincipal principal)

{

Thread.CurrentPrincipal = principal;

if (HttpContext.Current != null)

{

HttpContext.Current.User = principal;

}

}

For web-hosting, you must set the principal in both places; otherwise the security context may become inconsistent. For self-hosting, however, HttpContext.Current is null. To ensure your code is host-agnostic, therefore, check for null before assigning to HttpContext.Current, as shown.

Authorization

Authorization happens later in the pipeline, closer to the controller. That lets you make more granular choices when you grant access to resources.

  • Authorization filters run before the controller action. If the request is not authorized, the filter returns an error response, and the action is not invoked.
  • Within a controller action, you can get the current principal from the ApiController.User property. For example, you might filter a list of resources based on the user name, returning only those resources that belong to that user.

Using the [Authorize] Attribute

Web API provides a built-in authorization filter, . This filter checks whether the user is authenticated. If not, it returns HTTP status code 401 (Unauthorized), without invoking the action.

You can apply the filter globally, at the controller level, or at the level of inidivual actions.1

Globally: To restrict access for every Web API controller, add the AuthorizeAttribute filter to the global filter list:

C#Copy

public						static								void Register(HttpConfiguration config)

{

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

}

Controller: To restrict access for a specific controller, add the filter as an attribute to the controller:

C#Copy

// Require authorization for all actions on the controller.

[Authorize]

public						class								ValuesController : ApiController

{

public HttpResponseMessage Get(int id) { ... }

public HttpResponseMessage Post() { ... }

}

Action: To restrict access for specific actions, add the attribute to the action method:

C#Copy

public						class								ValuesController : ApiController

{

public HttpResponseMessage Get() { ... }

 

// Require authorization for a specific action.

[Authorize]

public HttpResponseMessage Post() { ... }

}

Alternatively, you can restrict the controller and then allow anonymous access to specific actions, by using the [AllowAnonymous] attribute. In the following example, the Post method is restricted, but the Get method allows anonymous access.

C#Copy

[Authorize]

public						class								ValuesController : ApiController

{

[AllowAnonymous]

public HttpResponseMessage Get() { ... }

 

public HttpResponseMessage Post() { ... }

}

In the previous examples, the filter allows any authenticated user to access the restricted methods; only anonymous users are kept out. You can also limit access to specific users or to users in specific roles:

C#Copy

// Restrict by user:

[Authorize(Users="Alice,Bob")]

public						class								ValuesController : ApiController

{

}

 

// Restrict by role:

[Authorize(Roles="Administrators")]

public						class								ValuesController : ApiController

{

}

Note

The AuthorizeAttribute filter for Web API controllers is located in the System.Web.Http namespace. There is a similar filter for MVC controllers in the System.Web.Mvc namespace, which is not compatible with Web API controllers.

Custom Authorization Filters

To write a custom authorization filter, derive from one of these types:

  • AuthorizeAttribute. Extend this class to perform authorization logic based on the current user and the user's roles.
  • AuthorizationFilterAttribute. Extend this class to perform synchronous authorization logic that is not necessarily based on the current user or role.
  • IAuthorizationFilter. Implement this interface to perform asynchronous authorization logic; for example, if your authorization logic makes asynchronous I/O or network calls. (If your authorization logic is CPU-bound, it is simpler to derive from AuthorizationFilterAttribute, because then you don't need to write an asynchronous method.)

The following diagram shows the class hierarchy for the AuthorizeAttribute class.

Authorization Inside a Controller Action

In some cases, you might allow a request to proceed, but change the behavior based on the principal. For example, the information that you return might change depending on the user's role. Within a controller method, you can get the current principle from the ApiController.User property.2

C#Copy

public HttpResponseMessage Get()

{

if (User.IsInRole("Administrators"))

{

// ...

}

}

 

From:

Note: about sessionStorage, can refer here:

转载于:https://www.cnblogs.com/time-is-life/p/7693576.html

你可能感兴趣的文章
Silverlight多文件(大文件)上传的开源项目
查看>>
HTML5网站大观:分享8个精美的 HTML5 网站案例
查看>>
php rewrite
查看>>
【转】从bundle中复制文件到Documents目录中的代码
查看>>
【转】UIWebView获取当前页面url的两种方法
查看>>
struts2中使用ajax so easy!!!
查看>>
Hibernate 事物隔离级别
查看>>
Linux ——记一记那恐怖的 rm -f
查看>>
C# 指针之美
查看>>
Oracle 10 参数配置说明
查看>>
解决'System.OutOfMemoryException' 的问题
查看>>
消息队列RabbitMQ和ActiveMQ的生产者流量控制
查看>>
再论 重载、覆盖、多态与函数隐藏
查看>>
Android 用户界面---菜单
查看>>
【学术报告】云山物罩 大话‘大数据’
查看>>
用Setup系列函数完成驱动卸载安装[驱动安装卸载程序]
查看>>
巧妙利用JQuery和Servlet来实现跨域请求
查看>>
JS中生成与解析JSON
查看>>
[开发记录]事件驱动中一种优雅的退出方法
查看>>
java对象转JSON JS取JSON数据
查看>>