Asp.net MVC 5 redirect account / login

I am learning ASP.NET MVC. I have MVC version 5.2.2.0

I have applied the Authorize attribute to the Index () action method in the Employee controller.

In the Web.config file, I changed the authentication tag details 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>

      

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

But it redirects to localhost: port / Account / Login

While researching other links I've 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 in Web.config

2. IIS 8 anonymous authentication from specific user to application pool id

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

<authentication mode="Windows" />

      

But nobody worked.

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

+3


source to share


3 answers


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

Open / AppStart / Startup.Auth.cs and edit the following code block to set up the target 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);

      

+5


source


Perhaps this has changed. I am learning ASP.NET 5 (dnx451 and MVC 6.0.0-rc1-final) and you need to define a default redirect address for service login: the "ConfigureServices" method not in the "Configure" method.



public void ConfigureServices(IServiceCollection services)
    {
            services.AddIdentity<IdentityUser, IdentityRole>(configure =>
        {
            //add some requirements
            configure.User.RequireUniqueEmail = true;
            configure.Password.RequiredLength = 8;
            //define the default page if a call must be [Autorized]
            configure.Cookies.ApplicationCookie.LoginPath =  "/Auth/Login"; 
        })
        .AddEntityFrameworkStores<AuthContext>(); //use entity framework to store the user data
}

      

+1


source


Comment out the following line in the web.config file, then it will not automatically go into "Account / Login". I have tested VS2015 and it works great.

Comment line:

<remove name="FormsAuthentication"/>

      

0


source







All Articles