c# - 将旧 Web 窗体移植到 OWIN 时获取 HTTP 401.2 未经授权

标签 c# asp.net authentication authorization owin

我关注了Require Authentication for all requests to an OWIN application所以我的代码看起来像这样:

public void Configuration(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseCookieAuthentication(new CookieAuthenticationOptions() { CookieSecure = CookieSecureOption.Never });
    app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions()
    {
        MetadataAddress = "https://login.windows.net/#####.onmicrosoft.com/federationmetadata/2007-06/federationmetadata.xml",
        Wtrealm = "http://localhost:33210/",
        // SignInAsAuthenticationType = // This is picked up automatically from the default set above
    });


    app.Use(async (context, next) =>
    {
        var user = context.Authentication.User;
        if (user == null || user.Identity == null || !user.Identity.IsAuthenticated)
        {
            context.Authentication.Challenge();
            return;
        }
        await next();
    });
}

该应用程序是 Web 窗体和 MVC 的混合体。我已经从 IIS 中删除了所有身份验证类型,删除了 authorizationmembershiproleManager 并设置了 authentication mode="None"web.config 中。

现在,当我访问站点时,无论是通过 Default.aspx、[Authorize] Controller ,还是通过强制质询的显式 AccountController:

public void SignIn()
{
    if (!Request.IsAuthenticated)
    {
        HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties { RedirectUri = "/" },
            WsFederationAuthenticationDefaults.AuthenticationType);
    }
}

所有这些都会导致 HTTP 错误 401.2 由于身份验证 header 无效,您无权查看此页面。我错过了什么?

最佳答案

问题是

I've removed all authentication types from IIS

至少需要在 IIS 中选择 Anonymous Authentication。使用 WebApp-OpenIDConnect-DotNet 等示例应用程序可能会重现相同的错误。通过在 IIS Express 的项目属性中禁用 Windows 和匿名身份验证。

关于c# - 将旧 Web 窗体移植到 OWIN 时获取 HTTP 401.2 未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28757481/

相关文章:

c# - 使用字符串作为 IEnumerable

c# - LINQ to Entities 不支持指定的类型成员 'Items'

asp.net - ASP.Net MVC 路由问题

django - 如何登录? Django TastyPie with ApiKeyAuthentication 实际认证过程

c# - 如何检查数组中对元素的数量?

c# - 我需要测试技巧

c# - 如何固定 GridView 中每列的宽度?

javascript - 将 .aspx 文件调用到另一个 .aspx 文件中

php - CodeIgniter:动态登录后重定向?

java - Vaadin 21 查看角色