diandian82的专栏

Welcome to my blog. Hope you can get what you want.

Add module to asp.net

If the user haven't logged in, no matter which page he requested, it will be transfer to Login Page.

namespace AuthorizationModule
...{
    
public class UserAuthorizationModule : IHttpModule
    
...{
        
public void Dispose()
        
...{ }

        
public void Init(HttpApplication context)
        
...{
            context.AcquireRequestState 
+= new EventHandler(context_AcquireRequestState);
        }


        
void context_AcquireRequestState(object sender, EventArgs e)
        
...{
            
// 获取应用程序
            HttpApplication application = (HttpApplication)sender;

            
// 检查用户是否已经登录
            if (application.Context.Session["UserName"== null || application.Context.Session["UserName"].ToString().Trim() == "")
            
...{
                
// 获取Url
                string requestUrl = application.Request.Url.ToString();
                
string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/'+ 1);

                
// 如果请求的页面不是登录页面,刚重定向到登录页面。
                if (requestPage != "Login.aspx")
                    application.Server.Transfer(
"Login.aspx");
            }

            
else
            
...{
                
// 已经登录,向每个请求的页面打印欢迎词。
                application.Response.Write(string.Format("欢迎您!{0}!", application.Context.Session["UserName"]));
            }

        }

    }

}

 

You must add this module to your application. In web.config, you have to add this sentence in System.web node.

<httpModules>
            
<add name="MyModule" type="AuthorizationModule.UserAuthorizationModule" />
        
</httpModules>
阅读更多
个人分类: ASP.net
想对作者说点什么? 我来说一句

asp.net mvc add edit delete

2011年06月09日 726KB 下载

没有更多推荐了,返回首页

不良信息举报

Add module to asp.net

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭