【问题标题】:Razor view calls wrong actionRazor 视图调用错误的操作
【发布时间】:2020-06-30 02:37:50
【问题描述】:

我正在尝试在我的 Asp.Net MVC 应用程序中使用 Auth0 身份验证。我有一个名为 Account 控制器的控制器和两个动作:注销和登录。

我从布局视图中调用它们,如果单击注销按钮,则应调用注销操作,如果单击登录按钮,则应调用登录操作。问题是我点击哪个按钮并不重要,它总是执行登录操作,也许有人知道吗?

AccountController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace my_diet.Controllers
{
    public class AccountController : Controller
    {
        [Authorize]
        public async Task Logout()
        {
            await HttpContext.SignOutAsync("Auth0", new AuthenticationProperties
            {
                // Indicate here where Auth0 should redirect the user after a logout.
                // Note that the resulting absolute Uri must be whitelisted in the
                // **Allowed Logout URLs** settings for the app.
                RedirectUri = Url.Action("Index", "Home")
            });
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        }
        public async Task Login(string returnUrl = "/")
        {
            await HttpContext.ChallengeAsync("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl });
        }

    }
}

_layouts.cshtml

<ul class="navbar-nav flex-grow-1">
@if (User.Identity.IsAuthenticated)
{
<li class="nav-item">
<a id="qsLogoutBtn" class="nav-link text-dark" asp-controller="Account" asp-action="Logout">Logout</a>
@*@Html.ActionLink("Logout", "Logout", "Account", new object { }, new { @class = "nav-link text-dark"})*@

</li>
}
else
{
<li class="nav-item">
<a id="qsLoginBtn" class="nav-link text-dark" asp-controller="Account" asp-action="x">Login</a>
</li>
}
</ul>

【问题讨论】:

    标签: asp.net-mvc authentication android-asynctask auth0 razor-pages


    【解决方案1】:

    试试这个。

    [AllowAnonymous]
    public async Task Login(string returnUrl = "/")
    {
       await HttpContext.ChallengeAsync("Auth0", new AuthenticationProperties() { 
       RedirectUri = returnUrl });
    }
    

    更多规范:

    您还可以使用 AllowAnonymous 属性来允许未经身份验证的用户访问单个操作。

    例如:

    [Authorize]
    
        public class AccountController : Controller
        {
            [AllowAnonymous]
            public ActionResult Login()
            {
            }
    
            public ActionResult Logout()
            {
            }
        }
    

    这将只允许经过身份验证的用户访问 AccountControllerLogin 操作除外,每个人都可以访问,无论他们是否经过身份验证或未经身份验证/匿名状态。

    警告:

    [AllowAnonymous] 绕过所有授权语句。如果将 [AllowAnonymous] 和任何 [Authorize] 属性结合使用,则 [Authorize] 属性将被忽略。例如,如果您在控制器级别应用 [AllowAnonymous],则同一控制器(或其中的任何操作)上的任何 [Authorize] 属性都会被忽略。

    【讨论】:

    • 谢谢!这似乎有效,不过,我认为您的意思是将其添加到 Logout 操作而不是 Login 之上。
    • 感谢您的解释!但在我的情况下,注销不起作用,它适用于 [AllowAnonymous]。那么它是否违反了安全性?因为未经授权的实体可以让我退出。
    • @KarolisPakalnis 实际上您只能在登录后注销。因此,如果您未登录并传递 logout url ,届时它将将用户重定向到登录页面。如果用户已登录,那么它将注销用户,清除会话并重定向到登录页面。不要使用 [AllowAnonymous] 属性进行注销。只有授权用户才能注销。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2020-11-17
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多