1

I'm getting the 401 Unauthorized error after user is redirected. enter image description here

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?

2
  • 1
    Have you got any [Authorize] attributes on the controller? Is the user calling SaveUser auth'd? Commented Jan 8, 2020 at 9:00
  • @scgough no attributes on the controller, user calling the SaveUser authorized Commented Jan 8, 2020 at 9:18

2 Answers 2

2

It is because you forget Authorize attribute on controller

[Authorize]
public class UserAccessManagement: Controller {
    public ActionResult Index() {
        return View();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Added the Authorize attribute but it's still not working. BTW Authorize Attribute added to the global filters
What is your actual scenario ? Are you adding new user and that newly added user will access the web ?
The scenario is user adds a new user and returns on either create page or page with users list. I guess what the new user will do is not relevant to the redirect error
okay, can you please convert MenuID to long and then pass to your new method. Hope it will works
1

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. But some of other actions with RedirectToAction were found by me and they worked. Besides versions hosted on another IIS had the same problem

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

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...

enter image description here

So on the Application_EndRequest the StatusCode is set to 401 and the error returned for the wrong type of the request

I guess searching for "401" would also help and if the constants were named they would have been found earlier

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.