【发布时间】: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