views:

87

answers:

2

Hi In my ASP.NET MVC project i have following tag in in web.config file

<authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="2880"/> </authentication>

This causes even the authenticated users but unauthorized resource requested users to redirect to logon page. but i need only to redirect this page if user try to access unauthorized page and not already authenticated(logged on) and redirect to custom page.

Is there easy way to do this without writing custom action filter?

+3  A: 

All that this line does in web.config is to simply define the timeout of the authentication cookie and the login url. It is your code that decides which parts of the site are authenticated or no, by for example decorating your controllers and/or actions with the [Authorize] attribute.

Darin Dimitrov
Hi Dimitrov,actually in my project I am writing actions like following [HttpPost] [Authorize(Roles = "Admin")] public ActionResult Edit(string id, FormCollection collection){ /**/}the point is if user not in "Admin" role but in some other role but he is already logged on that user also is being redirected to logon page with attribute in web config above mentioned.
DSharper
@DSharper: Yes, ASP.NET's not very good at handling 403 Forbidden messages - it's either "Allowed" or "Denied", and the roles don't come into it at that point.
Zhaph - Ben Duguid
A: 

please check your "authorization" setting in web.config file. It should be somewhat

<authorization>
      <deny users="?" />
      <allow users="*" />
    </authorization>

also when user authenticates successfully make sure you call

FormsAuthentication.SetAuthCookie(<username>, false); 
ajay_whiz