在注销按钮上无法访问控制器/操作,请单击

时间:2019-06-11 16:27:57

标签: c# html asp.net-mvc asp.net-identity

我已经在应用程序中使用了MVC模板,并根据需要对其进行了修改。单击“注销”按钮后,我应该能够在定义我的注销逻辑的地方执行注销操作。但是,当我单击按钮时,它直接将我带到登录屏幕,而无需在控制器中单击注销操作。

我尝试添加操作链接并明确指定要执行注销操作,但该操作也不起作用。 如果删除此操作,则会引发未找到的错误。我检查了提琴手工具,并在单击按钮时调用了此操作,然后将其重定向到“登录”,但是断点永远不会命中,或者如果我更改注销操作中的内容,则不会反映出来。 我还尝试对同一操作使用不同的名称,但未发现行为更改。

这是视图和控制器操作。

    // POST: /Account/LogOff
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        return RedirectToAction("Login", "Account");
    }

嵌入到布局中的局部视图:

    @if (1==1)
    {
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
    @Html.AntiForgeryToken()  

    <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <p class="navbar-text">Allturna's Insights Portal</p>
    </div>
    <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <p class="navbar-text">Hello!</p> @*Use username here from viewdata*@
                </li>
                <li>
                <li>
                    @Html.ActionLink("Tenants", "GetTenant", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, null)
                </li>
                <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
                <li><img src="~/Content/Image/Allturna_logo.png" height="50" width="220" /></li>
            </ul>
    </div>
}

}

RouteConfig.cs如下:

    public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
        );
    }
}

我无法弄清为什么注销按钮单击/提交表单无法完成注销操作。我将不胜感激。

在我从事这项工作时,我想补充一些注意事项。如果我使用带有身份的默认aspnet模板,它将到达注销按钮。但是,我尝试不使用身份来实现自己的登录和注销逻辑。那时候它没有退出。

我已经发布了两种逻辑(具有/不具有身份)均按预期达到注销状态。

以下带有身份的作品有效:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                //Add tenantId to session from user info (temporary testing with Allturna)
                Session["TenantId"] = 1;
                Session["AccessToken"] = "my token";
                return RedirectToLocal(returnUrl);

            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

当我使用以下登录逻辑(无身份)时,“注销”不会成功。

            [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        //get the authentication URL
        string strJwtToken = string.Empty;
        string strAuthenticationUrl = ConfigurationManager.AppSettings.Get("AuthenticationServerEndPoint");
        if (String.IsNullOrEmpty(strAuthenticationUrl))
            throw new Exception("Authentication Endpoint is not specified.");

        ServiceClient client = new ServiceClient(strAuthenticationUrl, "Authenticate");
        dynamic objAuthentication = new ExpandoObject();
        objAuthentication.UserName = model.Email;
        objAuthentication.Password = model.Password;
        client.AddJsonBody(objAuthentication);
        HttpStatusCode httpReturnCode = client.Post(out strJwtToken);

        if (httpReturnCode == HttpStatusCode.OK)
            Session["AccessToken"] = strJwtToken;

        return RedirectToLocal(returnUrl);
    }

我将需要了解身份框架/ SignInManager做了什么,使我的逻辑丢失了,以及如何将其添加到逻辑中并使注销正确工作。预先感谢。

0 个答案:

没有答案