Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi mates,

I'm onto a project that have multiple roles. There are few folders containing different pages. Folder access is limited to the individual roles. For now if a user in role 1 is trying to access the contents of the folder other than allowed, it redirects to Login page whereas I want it to redirect to a specific page saying, "You are not authorized to view this page" or like that. Should I do something with web.config file? Please suggest how can I achieve this purpose.
Any little help is appreciated.



Best Regards,
Sunny
Posted

A change in Web.Config file should do:
XML
<customErrors mode="On" defaultRedirect="/errors/404.aspx">
       <error statusCode="401" redirect="/errors/401.aspx"/><!--401 (Unauthorized)-->
       <error statusCode="403" redirect="/errors/403.aspx"/><!--403 (Forbidden)-->
       <error statusCode="404" redirect="/errors/404.aspx"/><!--404 (Not Found)-->
</customErrors>

Try out!
 
Share this answer
 
Comments
Sunny_Kumar_ 22-Sep-12 5:50am    
didn't work... redirects to the Login Page.
Sandeep Mewara 22-Sep-12 5:54am    
You have to be logged in in order to make this work.

First thing always is to check for authentication. Post that authorization.
Sunny_Kumar_ 22-Sep-12 6:00am    
I did was. I also tried like this:
if (Request.IsAuthenticated)
{
if(!User.IsInRole("role")) Response.Redirect("unAuthorized.aspx");
}
Sandeep Mewara 22-Sep-12 5:58am    
Still, if you seek for authorization, look at this:
http://stackoverflow.com/questions/11577614/redirect-unauthorized-users-to-a-custom-page-error

Details:
You can manipulate the content of "401 Access Denied" response (if this is the case) by adding the following code in the Application_EndRequest event of Global.asax.cs:

protected void Application_EndRequest(Object sender,
EventArgs e)
{
HttpContext context = HttpContext.Current;
if (context.Response.Status.Substring(0,3).Equals("401"))
{
context.Response.ClearContent();
context.Response.Write("<script language="javascript">" +
"self.location='../login.aspx';</script>");
}
}
Sunny_Kumar_ 22-Sep-12 6:01am    
gonna give this a try...
Here is the solution Forms Authentication - Redirecting users to a Page other than Default.aspx[^]

C#
// Once the user's entered credentials are verified //
if(Request.Params["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.text, false);
}
else
{
FormsAuthentication.SetAuthcookie(txtUserName.text, false);
Response.Redirect("CustomPage.aspx");
}

It may help.
 
Share this answer
 
Comments
Sandeep Mewara 22-Sep-12 5:29am    
Now, how again this is what OP is seeking for? He wants to direct people to 'unauthorized' page.

What you say is:
1. formAuthentication has to be there - NOT necessary
2. you redirect to login page! (authentication based)
There is a difference between authentication and authorization.

No where what OP is trying to do.

:doh:

BTW, I don't know if you downvoted my answer or not but I cannot upvote your answer as it is no where near to what OP asks.
I have not downvoted your answer...

Thanks...
Sunny_Kumar_ 22-Sep-12 6:11am    
copy cat:
http://geekswithblogs.net/ranganh/archive/2005/04/25/37612.aspx
I am not a copy cat, I have given the link and then quoted the answer...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900