Dispose (){}
Public void Init (httpapplication context)
{< br> context. authenticaterequest += DeleGate
{< br> iidentity = httpcontext. current. user. identity;
If(Identity= Null | !Identity. isauthenticated)
Return;
StringUsername=GetUserName (identity. Name );
If ( ! isvaliduser (username)
{< br> httpcontext. current. user = null ;
return ;
}
ienumerable myrole > roles = getroles (username);
myidentity = New myidentity (username, roles );
myprincipal = New myprincipal (myidentity );
Httpcontext. Current. User=Myprincipal;
};
}
Private Static String GetUserName ( String Fullname)
{
Int Separatorindex = Fullname. indexof ( ' \\ ' );
Return Fullname. substring (separatorindex + 1 );
}
Private Static BoolIsvaliduser (StringUsername)
{
//Replace following code with validation from database
Return False;
}
Private Static Ienumerable < Myrole > Getroles ( String Username)
{
// Replace here with your custom code. For example, get from database etc.
Return Null ;
}
}
[Serializable]
Public ClassMyidentity: iidentity
{
Private ReadonlyList<Myrole>Roles= NewList<Myrole>();
Public Myidentity ( String Name, ienumerable < Myrole > Roles)
{
If ( String . Isnullorempty (name )) Throw New Argumentnullexception ( " Name " );
This . name = name;
If (roles ! = null )
This . roles. addrange (roles);
}
Public string authenticationtype
{< br> Get { return " my authentication type " ;}
}
Public bool isauthenticated
{< br> Get { return true ;}
}
Public string name { Get ; private set ;}
PublicIlist<Myrole>Roles
{
Get{Return This. Roles ;}
}
//Add your custom code here
}
[serializable]
Public class myprincipal: iprincipal
{< br> private readonly myidentity identity;
Public myprincipal (myidentity identity)
{< br> If (identity = null ) throw New argumentnullexception ( " identity " );
This. Identity=Identity;
}
PublicIidentity identity
{
Get{Return This. Identity ;}
}
Public bool isinrole ( string role)
{< br> If ( string . isnullorempty (role) throw New argumentnullexception ( " role " );
return This . identity. roles. count (myrole => string . compare (myrole. name, role, tru E ) = 0 ) ! = 0 ;< BR >}
//Add your custom code here
}
[Serializable]
Public Class Myrole
{
Public Myrole ( String Name)
{
If ( String . Isnullorempty (name )) Throw New Argumentnullexception ( " Name " );
This. Name=Name;
}
Public StringName {Get;Private Set;}
//Add your custom code here
}
Configure web. config.
Web. config
<Httpmodules>
<AddName= "Myauthenticationmodule"Type= "Myauthenticationmodule"/>
</Httpmodules>