【问题标题】:c# mvc aspnet.core async Task<IActionResult>, RedirectToAciton not workingc# mvc aspnet.core async Task<IActionResult>, RedirectToAciton 不工作
【发布时间】:2022-01-23 17:35:52
【问题描述】:

我正在使用 MVC aspnetcore dotnetcore 6.0

我使用 LoginController 进行授权。当它成功时,我想将用户重定向到 HomeController。但是 RedirectToAction 不起作用。我的代码在这里:

[HttpPost]
    [AllowAnonymous]
    public async Task<IActionResult> UserLoginAsync(LoginRequestModel requestModel)
    {
        try
        {
            if (ModelState.IsValid)
            {
                LoginResponseModel loginResponseModel = new LoginResponseModel();
                var res = _wrapperService.Post<LoginRequestModel, LoginResponseModel>(requestModel, "/api/auth/login");
                var tokenRes = GetTokenInfo(res.Result.Data.Token);

                HttpContext.Session.SetString("username", requestModel.Email);
                var userIdentity = new ClaimsIdentity(tokenRes, "Login");
                ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(userIdentity);
                await HttpContext.SignInAsync(claimsPrincipal);
                return RedirectToAction("Index", "Home"); // I want to redirect this but it does not work. 
                return Ok(res.Result);
            }

            return BadRequest(ModelState);
        }
        catch (Exception e)
        {
            var errors = new HttpErrorViewModel()
            {
                Errors = new List<string>()
            };
            errors.Errors.Add(e.InnerException.Message);
            return BadRequest(errors);
        }
        
    }

我的 Ajax 代码在这里:

function LoginWithMail() {
        var inputs = {};
        $("#form-login .input-login").each(function () {
            inputs[$(this)[0].name] = $(this)[0].value;
        });
        $.ajax({
            method: "POST",
            url: "/Login/UserLogin/",
            data: inputs,
            dataType: 'json',
            beforeSend: function () {
                $("#Email").removeClass("border border-danger");
                $("#Password").removeClass("border border-danger");
                $("#LoginSpinner").show();
                $("#loginResMessages").html("").removeClass();
            },
            success: function (res) {
                console.log(res);
                $("#LoginSpinner").hide();

            },
            error: function (xhr, status, error) {
                $("#LoginSpinner").hide();

                $("#loginResMessages").html("");

我再说一遍。我想在成功时重定向此代码。我尝试了许多我找到的解决方案,但也没有奏效。 请帮忙。

【问题讨论】:

  • HomeController 的外观如何?您是否正在为您的案例创建 REST API?
  • HomeController 是:public IActionResult Index() { return View(); }
  • 您可能需要将返回值包装到这样的任务中,因为您将方法标记为async。
  • 你到底尝试了什么?因为在我看来你没有。你如何发送请求?错误是什么?错误发生在哪里?等等。你能解释一下吗?
  • @Ergis 没有错误。存在登录问题。你读过我写的吗?我再重复一遍。我想在这段代码中使用 RedirectToAction() 方法。它不工作。

标签: c# .net-core model-view-controller redirecttoaction aspnetcore-environment


【解决方案1】:

您是否使用 ajax 向登录操作发送请求?这可能是个问题。

如果您使用 ajax 发送 post 请求,只需在 ajax 方法中处理响应。如果成功,用js将窗口位置改为home/index。

【讨论】:

  • 我不会从 ajax 重定向 url。是的,我使用 ajax 发布。
  • 这是一个解决方案,但我不想使用。
  • 您想使用 ajax 重定向,而不是通过窗口位置。我很好奇还有什么其他“解决方案”
  • 如果我将 url 传递给 Ajax 成功结果,它不会造成安全漏洞吗?
  • 我为此尝试了不止一种解决方案,但我找不到。实际的安全漏洞是使用 ajax 发送用户登录信息。在 ajax 中做任何你想做的事,然后将表单提交给操作。然后你可以重定向到任何你想要的地方。
猜你喜欢
  • 2019-07-30
  • 2017-11-06
  • 2013-08-06
  • 2019-08-14
  • 2021-06-04
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 2018-03-08
相关资源
最近更新 更多