I am trying to make it easier to the developer,
Because there are multiple options of Authentication, and not all pages require authentication or full authentication.
In WebForms I can have a generic page,
Now each page inherits GenericPage, and only need to implemenet AuthorizationType (see code below)
Can I do anything similiar with Razor?
Or maybe I should somthing entirely different…
Here is the code in WebForms:
public class GenericPage : Page
{
public enum AuthorizationType
{
Users = 1,
AuthUsers = 2,
Admins = 4
}
public virtual bool IsAuth()
{
return Authenticator.IsAuth();
}
public virtual bool IsAdmin()
{
AuthUser authUser = Authenticator.GetAuthenticatedUser();
return (authUser != null && authUser.IsAdmin)
}
protected abstract AuthorizationType Authorization
{
get;
}
protected virtual string OnAuthorizationFailedUrl
{
get
{
return HomePageUrl;
}
}
protected void CheckAuthentication()
{
if (!IsUserAuthroized())
Response.Redirect(OnAuthorizationFailedUrl);
}
protected bool IsUserAuthroized()
{
AuthorizationType authorization = Authorization;
return (Authorization.Contains(AuthorizationType.Users) ||
(Authorization.Contains(AuthorizationType.AuthUsers) && IsAuth()) ||
(Authorization.Contains(AuthorizationType.Admins) && IsAdmin()));
}
override OnPageLoad()
{
CheckAuthentication();
}
}
Thanks in advance.
You can add this type of authorization as a global filter.
Filtering in asp.net mvc
http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html