asp.net IHttpModule模板使用者登陸代碼執行個體

來源:互聯網
上載者:User

asp教程.net ihttpmodule模板使用者登陸代碼執行個體

void application_acquirerequeststate(object source, eventargs e)
        {
            httpapplication application = (httpapplication)source;

            user user = (user)application.context.session["user"]; //擷取user
            if (user == null)
            {
                // application.context.server.transfer("webform1.aspx");
                string requesturl = application.request.url.tostring();
                string requestpage = requesturl.substring(requesturl.lastindexof('/') + 1);

                // 如果請求的頁面不是登入頁面,剛重新導向到登入頁面。

                if (requestpage != "login.aspx")
                   // application.server.transfer("ihttphandlertext/login/login.aspx");
                 application.response.redirect("login/login.aspx");
            }
            else
            {              
                application.response.write(string.format("歡迎您!{0}!", user.name));

            }
        }


%>

建立自訂 http 模組

ihttpmodule 介面
向實作類別提供模組初始化和處置事件。

命名空間:system.web
程式集:system.web(在 system.web.dll 中)

文法
public interface ihttpmodule

public interface ihttpmodule


public class helloworldmodule : ihttpmodule
{
    public helloworldmodule()
    {
    }

    public string modulename
    {
        get { return "helloworldmodule"; }
    }

    // in the init function, register for httpapplication
    // events by adding your handlers.
    public void init(httpapplication application)
    {
        application.beginrequest +=
            (new eventhandler(this.application_beginrequest));
        application.endrequest +=
            (new eventhandler(this.application_endrequest));
    }

    private void application_beginrequest(object source,
         eventargs e)
    {
    // create httpapplication and httpcontext objects to access
    // request and response properties.
        httpapplication application = (httpapplication)source;
        httpcontext context = application.context;
        context.response.write("<h1><font color=red>
            helloworldmodule: beginning of request
            </font></h1><hr>");
    }

    private void application_endrequest(object source, eventargs e)
    {
        httpapplication application = (httpapplication)source;
        httpcontext context = application.context;
        context.response.write("<hr><h1><font color=red>
            helloworldmodule: end of request</font></h1>");
    }

    public void dispose()
    {
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.