|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
A working demo of this code can be found here. This only uses the Custom authentication. However, the demo allows you to simply move to Windows authentication. IntroductionI've been developing a website where I wanted to use Windows authentication but had to cater for browsers that didn't support it. I looked for a possible solution and realized that you could merge Forms and Windows authentication. However, I didn't find a solution that fully met my needs, so I decided to develop my own solution. How it worksConfigurationThe XML code below is placed in the project's web.config file. This is the standard method of configuring the project for Forms authentication. <authentication mode="Forms">
<forms name="forms" loginUrl="login.aspx" timeout="15"></forms>
</authentication>
To set the permissions of a sub directory or file within the web project, the authorization information is enclosed within <location path="Private1.aspx">
<system.web>
<authorization>
<allow roles="low, medium, high" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="Private2.aspx">
<system.web>
<authorization>
<allow roles="medium, high" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="Private3.aspx">
<system.web>
<authorization>
<allow roles="high" />
<deny users="*" />
</authorization>
</system.web>
</location>
IUserAuthenticatorAll authenticators must implement the public interface IUserAuthenticator
{
UserAuthenticationData Authenticate(string username, string password);
UserAuthenticationData Authenticate(string username,
string password, string domain);
void AddRoles(UserAuthenticationData uad);
string Type{get;}
}
The
This Re-AuthenticationWithin the project's Gobal.asax ExtendedFormsAuthentication.ReAuthenticate(Context);
If Windows authentication is used, a new identity is created from the Code accessAs well as restricting access to locations within the web project, this method also allows you to place access security on methods or classes. The demo code below only allows those users that meet the requirements in terms of username or roles to access the method. If the user is not authorized then a [PrincipalPermissionAttribute(SecurityAction.Demand, Role="low")]
public static int Do1()
{
return 1;
}
[PrincipalPermissionAttribute(SecurityAction.Demand, Role="medium")]
public static int Do2()
{
return 2;
}
[PrincipalPermissionAttribute(SecurityAction.Demand, Role="high")]
public static int Do3()
{
return 3;
}
[PrincipalPermissionAttribute(SecurityAction.Demand, Name=@"domain\user")]
public static int DoWinUser()
{
return 4;
}
Update - 30 May 2004The code now uses a dummy cookie to the timeout value. Removes need for extra FormsAuthentication.SetAuthCookie("get_timeout", true);
DateTime expires =
FormsAuthentication.GetAuthCookie("get_timeout", true).Expires;
The timeout is placed as you would normally do with Forms authentication - within the <authentication mode="Forms">
<forms name="forms" loginUrl="login.aspx" timeout="15"></forms>
</authentication>
Mentions
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||