【问题标题】:AllowAnonymous only works intermittently in ASP.NET MVC 5AllowAnonymous 仅在 ASP.NET MVC 5 中间歇性工作
【发布时间】:2016-07-08 15:02:54
【问题描述】:

我的控制器操作之一拒绝进行身份验证,我不明白为什么。它只是 Visual Studio 中标准 MVC 5 项目模板中的标准 VerifyCode 操作,它看起来像这样:

[AllowAnonymous]
public async Task<ActionResult> VerifyCode(string provider, string returnUrl, bool rememberMe)
{
    if (!await SignInManager.HasBeenVerifiedAsync())
    {
        return View("Error");
    }
    return View(new VerifyCodeViewModel { Provider = provider, ReturnUrl = returnUrl, RememberMe = rememberMe });
}

每当我的应用程序在没有用户登录的情况下点击此控制器时,它会将用户返回到登录屏幕,即使它已被 AllowAnonymous 修饰。相比之下,这个标准的控制器动作:

[AllowAnonymous]
public ActionResult ForgotPassword()
{
    return View();
}

如果没有登录用户直接点击,效果很好。

为了弄清楚发生了什么,我在控制器中添加了以下测试操作:

// to see if it's down to the parameters
[AllowAnonymous]
public ActionResult VerifyCode()
{
    return View(new VerifyCodeViewModel { Provider = "", ReturnUrl = "", RememberMe = false });
}

// to see if it's down to the action name
[AllowAnonymous]
public ActionResult VerifyCod()
{
    return View(new VerifyCodeViewModel { Provider = "", ReturnUrl = "", RememberMe = false });
}

// to see if it's down to the viewmodel
[AllowAnonymous]
public ActionResult ForgotPassword()
{
    return View(new VerifyCodeViewModel { Provider = "", ReturnUrl = "", RememberMe = false });
}

点击其中的前两个会导致我的应用程序将我发送回登录页面。点击第三个可以毫无问题地呈现 ForgotPassword 视图。

我没有任何自定义授权过滤器。这里到底发生了什么?

【问题讨论】:

    标签: c# asp.net asp.net-mvc authentication owin


    【解决方案1】:

    检查您的 .vs/config/applicationhost.config 文件并验证以下部分是否设置为 Deny 和 true。

    <section name="anonymousAuthentication" overrideModeDefault="Deny" />
    <add name="AnonymousAuthenticationModule" lockItem="true" />
    

    【讨论】:

    • 找到了。它们按照描述设置。
    • 您在控制器本身上设置了哪些标志?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2020-03-16
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多