MVC4 Windows 身份验证重定向到帐户/登录 [英] MVC4 Windows Authentication Redirect to Account/Login

查看:24
本文介绍了MVC4 Windows 身份验证重定向到帐户/登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 2013IIS Express 开发服务器 在 MVC 4 应用程序中设置 Windows 身份验证.但是,我被重定向到/Account/Login(就像我使用表单身份验证一样).

I am setting up Windows Authentication in an MVC 4 application using Visual Studio 2013 and using the IIS Express Development Server. However, I get redirected to /Account/Login (as if I were using forms authentication).

我的 bin 文件夹(或任何地方)中没有提及 WebMatrix,如下所述:IIS 上的 ASP.NET MVC3 和 Windows Auth 不断重定向到/Account/Login.

I have no reference to WebMatrix in my bin folder (or anywhere) as described here: ASP.NET MVC3 and Windows Auth on IIS keeps redirecting to /Account/Login.

我已按照这篇文章的建议将这些条目添加到 web.config 中的 appSettings 中:MVC5 在使用 Windows 身份验证时重定向到 Login.aspx

I have added these entries to appSettings in the web.config as suggested by this post: MVC5 Redirects to Login.aspx when using Windows Authentication

<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>

这是我目前所做的:

在 system.web 中添加了 Windows 身份验证.

Added windows authentication to system.web.

<authentication mode="Windows"/>

为我的控制器添加了 [Authorize] 属性.我还尝试使用 web.config 中的授权标记而不是控制器上的属性(拒绝用户 =?").结果是一样的.

Added the [Authorize] attribute to my controller. I have also tried using the authorization tag in the web.config instead of the attribute on the controller (deny users="?"). The results are the same.

对于我项目中的开发服务器设置,我更改了这些设置:

For the Development Server settings in my project I changed these settings:

匿名身份验证 = 已禁用

Anonymous Authentication = Disabled

Windows 身份验证 = 已启用

Windows Authentication = Enabled

我找不到对 FormsAuthentication 的引用.我不明白为什么它仍然重定向到一个不存在的登录页面.

I can find no reference to FormsAuthentication. I can't figure out why it still redirects to a non-existent login page.

推荐答案

在创建项目时,它可能是使用在项目的 App_Start 文件夹中添加了 Startup.Auth 的模板完成的.(如果您没有在创建新的 ASP.Net 项目对话框中将其更改为 Windows 身份验证作为身份验证方法,则默认模板使用个人用户帐户)

When the project was created it may have been done using a template that added Startup.Auth in the App_Start folder in your project. (The default template uses Individual User Accounts if you did not change it to windows authentication as the Authentication method in the create new ASP.Net Project dialog)

尝试注释掉这些行(如果它们存在)

Try commenting out these lines if they are present

   app.CreatePerOwinContext(ApplicationDbContext.Create);
   app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);


   app.UseCookieAuthentication(new CookieAuthenticationOptions
   {
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
      LoginPath = new PathString("/Account/Login"),
      Provider = new CookieAuthenticationProvider
      {
          OnValidateIdentity =  SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
               validateInterval: TimeSpan.FromMinutes(30),
               regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
      }
  });

  app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

或者如果你没有向这个文件添加任何东西,你可以完全删除它并调用它

Or if you have not added anything to this file you could remove it completely and the call to it

        ConfigureAuth(app);

在项目根目录下的startup.cs中找到

found in the startup.cs in the root of the project

现在大多数帐户控制器在这种情况下都不好用,所以也要准备好清理它.

Now most of the account controller is no good to use if this case so be prepared to clean that up also.

这一行在 web 配置中很重要且正确

This line is important and correct in the web config

<authentication mode="Windows"/>

这些行可能与问题没有直接关系,可以删除

these lines are probably not directly related to the issue and can be removed

<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>

其他开发设置也正确.

这篇关于MVC4 Windows 身份验证重定向到帐户/登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆