[转]OData and Authentication – Part 5 – Custom HttpModules
In the last post we saw how to add custom authentication inside your Data Service using the ProcessingRequest event.
Unfortunately that approach means authentication is not integrated or shared with the rest of your website.
Which means for all but the simplest scenarios a better approach is needed: HttpModules.
HttpModules can do all sort of things, including Authentication, and have the ability to intercept all requests to the website, essentially sitting under your Data Service.
This means you can remove all authentication logic from your Data Service. And create a HttpModule to protect everything on your website – including your Data Service.
Built-in Authentication Modules:
Thankfully IIS ships with a number of Authentication HttpModules:
- Windows Authentication
- Form Authentication
- Basic Authentication
You just need to enable the correct one and IIS will do the rest.
So by the time your request hits your Data Service the user with be authenticated.
Creating a Custom Authentication Module:
If however you need another authentication scheme you need to create and register a custom HttpModule.
So lets take our – incredibly naive – authentication logic from Part 4 and turn it into a HttpModule.
First we need a class that implements IHttpModule, and hooks up to the AuthenticateRequest event something like this:
public class CustomAuthenticationModule: IHttpModule
{
public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
new EventHandler(context_AuthenticateRequest);
}
void context_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (!CustomAuthenticationProvider.Authenticate(app.Context))
{
app.Context.Response.Status = “ Unauthorized”;
app.Context.Response.StatusCode = ;
app.Context.Response.End();
}
}
public void Dispose() { }
}
We rely on the CustomAuthenticationProvider.Authenticate(..) method that we wrote in Part 4 to provide the actual authentication logic.
Finally we need to tell IIS to load our HttpModule, by adding this to our web.config:
<system.webServer>
<modules>
<add name=”CustomAuthenticationModule”
type=”SimpleService.CustomAuthenticationModule”/>
</modules>
</system.webServer>
Now when we try to access our Data Service – and the rest of the website – it should be protected by our HttpModule.
NOTE: If it this doesn’t work, you might have IIS 6 or 7 running in classic mode which requires slightly different configuration.
Summary.
In part 2 we looked about using Windows Authentication. And in parts 3, 4 and 5 we covered all the hooks available to Authentication logic in Data Services, and discovered that pretty much everything you need to do is possible.
Great.
Next we’ll focus on real world scenarios like:
- Forms Authentication
- Custom Basic Authentication
- OAuthWrap
- OAuth 2.0
- OpenId
- etc…
Alex James Program Manager Microsoft
[转]OData and Authentication – Part 5 – Custom HttpModules的更多相关文章
- [转]OData and Authentication – Part 6 – Custom Basic Authentication
本文转自:https://blogs.msdn.microsoft.com/astoriateam/2010/07/21/odata-and-authentication-part-6-custom- ...
- Java EE 7 / JAX-RS 2.0: Simple REST API Authentication & Authorization with Custom HTTP Header--reference
REST has made a lot of conveniences when it comes to implementing web services with the already avai ...
- Using HiveServer2 - Authentication
To configure Hive for use with HiveServer2, include the following configuration properties in the .. ...
- web.config配置详细说明
(一).Web.Config是以XML文件规范存储,配置文件分为以下格式 1.配置节处理程序声明 特点:位于配置文件的顶部,包含在<configSections>标志中. 2.特定应 ...
- [转帖]IIS内虚拟站点配置信息说明
web.config配置详细说明 https://www.cnblogs.com/zhangxiaolei521/p/5600607.html 原作者总结的很详细 但是没有完全的看完 自己对IIS 的 ...
- Web.Config详细说明
(一).Web.Config是以XML文件规范存储,配置文件分为以下格式 1.配置节处理程序声明 特点:位于配置文件的顶部,包含在<configSections>标志中. 2.特定应 ...
- Chapter 5: Design and implement security
Configure authentication Authenticating users IIS authentication Anonymous ASP.net impersonation Bas ...
- laravel/lumen 单元测试
Testing Introduction Application Testing Interacting With Your Application Testing JSON APIs Session ...
- 匿名访问ReportService报表服务器(一)
我的数据库版本是sql server 2008 r2,系统环境是windows server2008. 对于sql server 2008 r2上报rs报表的匿名访问问题,我这边尝试过两个可行方案: ...
随机推荐
- WPF显示Gif动画
WPF的Image控件不能很好的支持.gif文件.解决办法有如下2种. 1使用MediaElement <MediaElement Source="file://D:\anim.gif ...
- [UWP开发]在windows10中设置壁纸~UserProfilePersonalizationSettings
在之前的wp8.1和wp8中,微软没有公开设置壁纸的API,只有一个设置锁屏的API,但在Windows10中,微软为我们提供了设置壁纸的API:TrySetWallpaperImageAsync,他 ...
- docker pull manifest unknown blob errors
问题:docker pull manifest unknown blob errors 原因:公司网络是代理模式,所以我的 docker 服务配置成proxy模式: [root@localhost ~ ...
- Android Studion有用的快捷键
注释:ctrl+/ 如果代码未添加注释,则添加注释上去:否则取消已经注释. 格式化代码:ctrl+alt+L l键不需要大写锁定,这里只是为了更清楚表示是L键.
- G - 確率(水题)
原文链接 G - 確率 Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit S ...
- iOS没你想的那么安全?
iOS应用由于直接运行在用户的手机上,而不是运行在后台服务器上,所以更容易被攻击. 任何系统都会有木马病毒的产生,不存在绝对的安全,iOS应用由于直接运行在用户的手机上,而不是运行在后台服务器上,所以 ...
- html中object和embed标签的区别
♦object定义一个嵌入的对象.请使用此元素向您的 XHTML 页面添加多媒体.此元素允许您规定插入 HTML 文档中的对象的数据和参数,以及可用来显示和操作数据的代码. ♦<object&g ...
- 如何在CentOS 7上使用vsftpd(FTP)的配置文件介绍
vsftpd.conf - vsftpd的配置文件. 描述 vsftpd.conf可用于控制vsftpd行为的各个方面. 默认情况下,vsftpd在/etc/vsftpd.conf位置查找此文件. 但 ...
- leetcode-695-Max Area of Island(BFS)
题目描述: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land ...
- leetcode-350-Intersection of Two Arrays II(求两个数组的交集)
题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...