Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC 5 redirect to Account/Login

I'm learning ASP.NET MVC. I have MVC version 5.2.2.0

I attached Authorize attribute to an action method Index() in Employee controller.

In the Web.config file, I changed authentication tag data as follows:

<system.web>
    <authentication mode="Forms">
        <forms loginurl="~/Authentication/Login"></forms>
    </authentication>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
</system.web>

What is expected is that when localhost:port/Employee/Index is accessed, the user should be redirected to localhost:port/Authentication/Login

But it is redirecting to localhost:port/Account/Login

By looking at other links, I tried the following things:

1.Added

<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>
<add key="loginUrl" value="~/Authentication/Login" />
<add key="PreserveLoginUrl" value="true" />

to appSettings section of Web.config

2.Changed IIS 8 Anonymous Authentication from Specific User to Application Pool Identity

3.When the above two didn't work, I changed authentication tag to

<authentication mode="Windows" />

But none worked.

EDIT Don't do the things 1, 2, 3 I mentioned above. Just do the changes mentioned in the answer

like image 971
Engineer Avatar asked Jul 04 '15 10:07

Engineer


People also ask

What is redirect to action in MVC?

RedirectToAction(String, Object) Redirects to the specified action using the action name and route values. RedirectToAction(String, String) Redirects to the specified action using the action name and controller name.

What is Authorize attribute in MVC?

In MVC, the 'Authorize' attribute handles both authentication and authorization. In general, it works well, with the help of extension to handle AJAX calls elegantly, and to distinguish between unauthorized users and those who are not logged in.


1 Answers

The problem is that you will have the OWIN middleware configured by default to redirect to "/Account/Login" for cookie authentication.

Open /AppStart/Startup.Auth.cs and edit the following block of code to target our own URL :-

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
like image 65
Derek Avatar answered Sep 27 '22 22:09

Derek