ASP.NET Web API Authorization using Tokens
Planning real world REST API
http://blog.developers.ba/post/2012/03/03/ASPNET-Web-API-Authorization-using-Tokens.aspx
When you try to plan how to build real world REST API like other major players like Facebook or Foursquare have you will soon realize that all major players use OAuth 2.0 .
ASP.NET Web API comes with support for authorize attribute and that’s nice, but for real world API I want to support token based approach.
OAuth 2.0 Server
For supporting token based approach you must have some kind of server that will issue tokens. Building token server can be complex and most major players have implemented OAuth 2.0 server based on draft 10 OAuth documentation.
We hope that Microsoft will provide us with their own OAuth 2.0 server for free in final version of ASP.NET MVC 4.
Meanwhile I will just assume that you already have your own OAuth 2.0 server.
Building ActionFilterAttribute
I have solved my problem with authorization by implementing RequireAuthorize ActionFilterAttribute. This attribute also have scope property. Scope property is used for limiting access to your REST API.
You just need to decorate controllers or actions in controllers with this attribute and optionally set required scope for accessing these actions.
Here is RequireAuthorizeAtribute:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
public class RequireAuthorization : ActionFilterAttribute { public string Scope { get ; set ; } public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { string [] scope = null ; if (! string .IsNullOrEmpty(Scope)) { scope = Scope.Split( new [] { "," }, StringSplitOptions.RemoveEmptyEntries); } string query = actionContext.Request.RequestUri.Query; string accessToken = HttpUtility.ParseQueryString(query).Get( "accessToken" ); // we first check for valid token if (accessToken != null ) { IAccessTokenValidator accessTokenValidator = new AccessTokenValidator(); bool validToken = accessTokenValidator.ValidateToken(accessToken, scope); if (!validToken) { var response = new HttpResponseMessage { Content = new StringContent( "This token is not valid, please refresh token or obtain valid token!" ), StatusCode = HttpStatusCode.Unauthorized }; throw new HttpResponseException(response); } } else { var response = new HttpResponseMessage { Content = new StringContent( "You must supply valid token to access method!" ), StatusCode = HttpStatusCode.Unauthorized }; throw new HttpResponseException(response); } base .OnActionExecuting(actionContext); } } |
And here is AccessTokenValidator class:
1
2
3
4
5
6
7
8
9
10
11
12
|
public class AccessTokenValidator : IAccessTokenValidator { public bool ValidateToken( string token, string [] scope) { // replace this logic with dataBase access to table with tokens if (token != "someToken" ) { return false ; } return true ; } } |
ASP.NET Web API Authorization using Tokens的更多相关文章
- Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)
http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-an ...
- Authentication and Authorization in ASP.NET Web API
You've created a web API, but now you want to control access to it. In this series of articles, we ...
- ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 Part 5 (by TAISEER)
https://www.cnblogs.com/KimmyLee/p/6430474.html https://www.cnblogs.com/rocketRobin/p/9077523.html h ...
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...
- [转] JSON Web Token in ASP.NET Web API 2 using Owin
本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...
- JSON Web Token in ASP.NET Web API 2 using Owin
In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...
- 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(2)
chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...
- ASP.NET Web API 2 external logins with Facebook and Google in AngularJS app
转载:http://bitoftech.net/2014/08/11/asp-net-web-api-2-external-logins-social-logins-facebook-google-a ...
- 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)
在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...
随机推荐
- Unix/Linux环境C编程入门教程(30) 字符串操作那些事儿
函数介绍 rindex(查找字符串中最后一个出现的指定字符) 相关函数 index,memchr,strchr,strrchr 表头文件 #include<string.h> 定义函数 c ...
- HDU2084 数塔 (DP入门题)
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- Red5 1.0 RC1 与tomcat 6 整合
1.0以上版本没整合成功过,如有人整合成功过,也分享下,在此先谢谢 一.下载red5-1.0.0-RC1.zip 下载地址:http://code.google.com/p/red5/ 和http:/ ...
- Swift中的集合类型
一.引子: 在2014年10月TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人 员的追捧,其热衷程度并不亚于当红巨星Tay ...
- Oracle11g重建EM 报ORA-20001: SYSMAN already exists
日志: Apr , :: PM oracle.sysman.emcp.EMReposConfig createRepository : SYSMAN already exists.. ORA-0651 ...
- c++单元测试框架googletest
一.概述 Googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用在windows.linux.Mac等OS平台上: 代码框架: [root@docker googletest-re ...
- jmx使用应该注意的基本规范
1.标准MBean 名称必需是在要监控的类名后面加上“MBean ”. 2.监控的类和MBean 接口必需在同一包下,也可以理解为注册的接口名字必须以MBean结尾,接口实现类比接口名字少了MBean ...
- 我的第一个html计算器
html代码. <!DOCTYPE HTML> <html> <head> <style type="text/css"> body ...
- struts 2 debug标签隐藏不显示
struts2 的标签debug在页面中应用,并且struts的配置文件中也设置为开发模式,但是这个标签却被隐藏了,究其原因,是因为页面中body元素生命了class,其样式覆盖了原来的样式. 比如: ...
- FMDatabase 数据库的使用
创建,插入,更新和删除:使用executeUpdate方法,而查询则用executeQuery 1.实例化FMDatabase //paths: ios下Document路径,Document为ios ...