谁有用 HttpModule 写的认证程序??要完整的,不要网上的例子 谢谢
解决方案 »
- 关于下拉菜单实现分页
- 子类的Page_Load没有被触发
- aspnetpager 中如何使用URL 参数
- 根据四酷全书 ASP.NET 新闻发布系统CMS 修改 在vs2005+sql2005上运行,另附设计文档
- 菜鸟有一问相求:做asp.net网站的整体思路!!!!!!!!!!!
- net frame问题
- 请问如何在后台编码中动态添加控件
- 如何动态的改变<IMG>的src属性?
- 为什么.aspx里link不起作用而style却可以?
- 为什么我这个跨网页传递一直报错!未将对象实例化??
- 如何整合ASP.NET与Dreamweave软件开发网站!
- VS.net怎么把默认保存文件格式设为UTF-8格式![问题解决立马结贴]
using System;
using System.Web;
using System.Security.Principal;
namespace MyModules
{
public class CustomModule: IHttpModule
{
public CustomModule(){}
public void Dispose(){}
public void Init(HttpApplication app){
//建立安全模块
app.AuthenticateRequest +=new EventHandler(this.AuthenticateRequest) ;
} private void AuthenticateRequest(object o,EventArgs e)
{
HttpApplication app=(HttpApplication)o;
HttpContext content=(HttpContext)app.Context ; if((app.Request["userid"]==null)||(app.Request["password"]==null)){
content.Response.Write("未提供必需的参数!!") ;
content.Response.End() ;
} string userid=app.Request["userid"].ToString();
string password=app.Request["password"].ToString();
string[] strRoles=AuthenticateAndGetRoles(userid, password);
if((strRoles==null)||(strRoles.GetLength(0)==0)){
content.Response.Write("未找到相配的角色!!") ;
app.CompleteRequest() ;
}
GenericIdentity objIdentity=new GenericIdentity(userid,"CustomAuthentication");
content.User=new GenericPrincipal(objIdentity,strRoles) ;
} private string[] AuthenticateAndGetRoles(string r_strUserID,string r_strPassword){
string[] strRoles=null ;
if((r_strUserID.Equals("Steve"))&&(r_strPassword.Equals("15seconds"))){
strRoles=new String[1] ;
strRoles[0]="Administrator" ;
}
else if ((r_strUserID.Equals("Mansoor"))&&(r_strPassword.Equals("mas"))){
strRoles=new string[1] ;
strRoles[0]="User" ;
}
return strRoles ;
}
}
}
编译后生成:Custom.dll编辑Web.config文件:
<system.web>
<httpModules>
<add name="Custom" type="MyModules.CustomModule,Custom"/>
</httpModules>
</system.web>--
Custom.aspx页面内容:<script language="c#" runat="server">
public void page_load(Object obj,EventArgs e)
{
lblMessage.Text = "<H1>Hi, " + User.Identity.Name + "</H1>";
if(User.IsInRole("Administrator"))
lblRole.Text="<H1>You are an Administrator</H1>";
else if(User.IsInRole("User"))
lblRole.Text = "<H1>You are a normal user</H1>";
}
</script>
<form runat="server">
<asp:Label id="lblMessage" forecolor="red" font-size="10pt" runat="server"/>
<asp:Label id="lblRole" forecolor="red" font-size="10pt" runat="server"/>
</form>