【问题标题】:ASP.Net Form authentication 401.2 UnauthorizedASP.Net 表单认证 401.2 未授权
【发布时间】:2017-07-15 18:57:01
【问题描述】:

我有一个让我发疯的问题。我在 Visual Studio 2015 中使用预定义模板创建了一个 ASP.Net 项目。目标框架是4.5

当我添加表单身份验证时,我得到的只是 401.2 消息。

这是我配置应用程序的方式: 我创建了一个 Login.aspx 页面。然后在 web.config 中,我在 system.web 部分添加了以下属性:

 <authentication mode="Forms">
      <forms loginUrl="Login.aspx"
             protection="All"
             timeout="30"
             name=".ASPXAUTH"
             path="/"
             defaultUrl="Default.aspx" />
    </authentication>
    <authorization>
      <deny users="?"/>
      <allow users="*"/>
    </authorization>

我查看了其他一些帖子,但到目前为止没有一个帮助我:

这一项不适用: Access is Denied - 401.2: Unauthorized Error

这个看起来很有希望,但我的应用没有雪茄: .NET Forms Authentication using IIS8.0 Express (vs2013) - 401.2.: Unauthorized: Logon failed due to server configuration

这个让我想到: https://stackoverflow.com/a/35925125/980201

当我检查 IIS(Express) 日志文件时,这是我得到的:

2017-02-24 19:29:22 ::1 GET / .... - 302
2017-02-24 19:29:22 ::1 GET /Login ReturnUrl=%2f .... - 401

Login.aspx 中的 .aspx 去哪儿了?

所以我删除了所有 FriendlyURL 的东西,但又没有运气了。在最后一次尝试中,我将配置更改为:

<forms loginUrl="LoginXXX.aspx"

这就是我得到的:

2017-02-24 19:45:37 ::1 GET / ... - 302
2017-02-24 19:45:37 ::1 GET /LoginXXX.aspx ReturnUrl=%2f 57745 - 404

为什么 .Net 重定向到 login 而不是 login.aspx ? 对我的应用和配置文件有什么其他想法吗?

非常感谢!

问候罗兰

编辑 一些意外的转折:当我在 VS 中使用 Login.aspx.cs 作为当前选项卡开始调试时,它失败了。当我从 Default.aspx.cs 开始调试时,它可以工作。将我的项目的 Start Action 设置为 Default.aspx 解决了这个问题。但是,当站点运行时,将 url 更改为 login.aspx 会使其再次失败。

为什么?对我来说,只有每个人都可以看到的页面不能直接导航,这没有任何意义。

【问题讨论】:

  • 您是否检查过 IIS 中是否启用了匿名身份验证?
  • 嗨 Ghini,是的。在 applicationhost.config 中是允许的。
  • 这个答案为我解决了这个问题:stackoverflow.com/a/51797308/538763

标签: c# asp.net .net visual-studio


【解决方案1】:

如果您使用 Visual Studio 创建了一个默认项目,然后您使用它来启用表单身份验证,那么问题是默认情况下禁用了表单身份验证,因为 Identity Framework 是 VS 2015 中的默认身份验证机制。

在 web.config 中查找这个条目

<system.webServer>
    <modules>
        <remove name="FormsAuthentication" />
    </modules>
</system.webServer>

并删除“删除”。

【讨论】:

  • 嗨 Erik,这个条目不在我的 web.config 中
猜你喜欢
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
相关资源
最近更新 更多