Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorize login URL in asp.net MVC 3

I am working on an Asp.Net MVC 3 application. I have created admin area for the website and applied [Authorized] attribute to actionmethods after login. When I try to access these urls directly without login like admin/home or admin/productlist, I am redirected to /Home/Login with authentication error. I want to redirect to Admin/Login.

Please suggest. Thanks

like image 347
DotnetSparrow Avatar asked Oct 01 '11 01:10

DotnetSparrow


People also ask

What is authorization in ASP NET Core MVC?

View-based authorization in ASP.NET Core MVC. A developer often wants to show, hide, or otherwise modify a UI based on the current user identity. You can access the authorization service within MVC views via dependency injection.

How to log out from a form authentication in ASP NET MVC?

To log out from a form authentication in ASP NET MVC, we need to call the form authentication sign out using the code below. IX. Create Login and Register View To create a view Just follow the steps below.

What is composing authorization attribute in ASP NET?

Composing authorization attributes results in authentication and authorization executing multiple times when you have one more AuthorizeAttribute or AuthorizeFilter instances also applied to the page. Work in conjunction with the rest of ASP.NET Core authentication and authorization system.

How to add authorizationfilter in MVC?

Add new empty MVC project. Then add a new controller in it. if (! (String.IsNullOrEmpty (name))) After adding the code to the controller and making views add a new class name, AuthorizationFilter, in App_Start Folder.


2 Answers

If this is a Stock MVC 3 Authorization then myself as well as many others have had problems with the incorrect url address being set for the "LogOn" Action... For some reason authorize is trying to send a user to Account\Login and looking at the account views tells that there is actually no "Login" view it is called "LogOn" so you have to fix this in the Web.config file with the following:

                <add key="loginUrl" value="~/Account/LogOn" />
like image 107
Skindeep2366 Avatar answered Oct 10 '22 19:10

Skindeep2366


The login URL for ASP.NET applications (including MVC3 ones) is controlled in web.config, in the forms authentication section:

<configuration>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Home/Login" timeout="2880" />
    </authentication>
  </system.web>
</configuration>

The trick for you is that you want two different login URLs. ASP.NET has a great feature where you can have a web.config file in each directory of your project, and as needed it will use the most specific setting it can find, up to the root web.config. So in the folder where you have your admin views ("Admin" I'm guessing), you should be able to create a second web.config, which will apply only to those pages and lower in the tree:

<configuration>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Admin/Login" timeout="2880" />
    </authentication>
  </system.web>
</configuration>
like image 30
David Pope Avatar answered Oct 10 '22 21:10

David Pope