zoukankan      html  css  js  c++  java
  • [转]OData and Authentication – Part 5 – Custom HttpModules

    本文转自:https://blogs.msdn.microsoft.com/odatateam/2010/07/19/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 = “401 Unauthorized”; 
                app.Context.Response.StatusCode = 401; 
                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:

    Alex James Program Manager Microsoft

  • 相关阅读:
    Java中顺序、并行与并发
    Java设计模式之Iterator
    渗透基础流程思路丶技巧丶与总结
    阿里巴巴Java开发手册之并发处理注意事项
    Java中Thread方法启动线程
    IOCP之客户端及消息传递
    IOCP简单实现
    JAVA 递归线程池测试 ExecutorService / ForkJoinPool
    TCP与UDP的一些心得
    七.badboy检查点和参数化
  • 原文地址:https://www.cnblogs.com/freeliver54/p/6961711.html
Copyright © 2011-2022 走看看