我关注了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 中删除了所有身份验证类型,删除了 authorization
、membership
、roleManager
并设置了 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/