简体   繁体   English

重定向后asp.net mvc 401未经授权的错误

[英]asp.net mvc 401 Unauthorized error after redirect

I'm getting the 401 Unauthorized error after user is redirected.用户重定向后,我收到 401 Unauthorized 错误。 在此处输入图片说明

User is being redirected to another page after a new user created.创建新用户后,用户被重定向到另一个页面。

public ActionResult SaveUser(UserViewModel userViewModel)
{
    ModelState.Remove("IsDSA");
    ModelState.Remove("IsAccountRepresentative");

    var savedUser = SaveOrUpdateUser(ref userViewModel);

    TempData["Status"] = ViewBag.Status;
    TempData.Keep("Status");

    var MenuId = Request.QueryString["MenuID"];

    TempData["MenuId"] = MenuId;
    TempData.Keep("MenuId");

    if (userViewModel.AddAnotherUserRequseted && savedUser != null)
    {

        return RedirectToAction("CreateNewUser", new { MenuID = Request.QueryString["MenuID"] });
    }

    return RedirectToAction("UserAccessManagement", "UserAccessManagement", new { MenuID = Request.QueryString["MenuID"] });
}

public ActionResult UserAccessManagement(string TabName, long MenuID)
{
    ...
}

How can I fix this error?我该如何解决这个错误? Maybe the reason is that authentication cookies are not sent with the redirect?也许原因是身份验证 cookie 没有随重定向一起发送?

It is because you forget Authorize attribute on controller这是因为您忘记了控制器上的 Authorize 属性

[Authorize]
public class UserAccessManagement: Controller {
    public ActionResult Index() {
        return View();
    }
}

So after a full day of investigation I started to doubt that reason may be in the Authorize Attribute or global filters and began thinking that maybe IIS somehow returns 401 on the redirect requests.因此,经过一整天的调查,我开始怀疑原因可能出在授权属性或全局过滤器中,并开始认为 IIS 可能会以某种方式在重定向请求上返回 401。 But some of other actions with RedirectToAction were found by me and they worked.但是我发现了 RedirectToAction 的其他一些操作并且它们起作用了。 Besides versions hosted on another IIS had the same problem除了托管在另一个 IIS 上的版本也有同样的问题

Then I started to wonder if there is any Authorization configuration in the MVC project other then default and searched through the project "authorize" which didn't give any unexpected results然后我开始怀疑MVC项目中是否有除default之外的其他Authorization配置,并搜索了“authorize”项目,没有给出任何意外结果

But then an idea came up to me to search through all the solution the "redirect" phrase and I finally found the root of the issue...但是后来我想到了一个想法来搜索“重定向”短语的所有解决方案,我终于找到了问题的根源......

在此处输入图片说明

So on the Application_EndRequest the StatusCode is set to 401 and the error returned for the wrong type of the request因此,在 Application_EndRequest 上,StatusCode 设置为 401,并且为错误的请求类型返回了错误

I guess searching for "401" would also help and if the constants were named they would have been found earlier我想搜索“401”也会有所帮助,如果常量被命名,它们会更早被找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM