0

If CurretUserToken is null i am redirecting to LoginPage, but it is redirecting on current layout, i want to render on LoginLayout page

public override void OnAuthorization(AuthorizationContext filterContext)
    {
       if(filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) 
           ||  filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
           return;

       CurrentUserToken = sessionManager.CurrentUserToken;

       if (string.IsNullOrEmpty(CurrentUserToken))
           HandleUnauthorizedRequest(filterContext);
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Action = "SignIn", Controller = "Account" }));
    }

and this is my controller to which i am redirecting when CurrentUserToken is Null

     [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> SignIn(LoginModel loginModel, string actionType)
    {
        if (actionType == "Login")
        {
            if (loginModel.RememberMe && CheckUserCookie()==null)
            {
                InsertCookie(loginModel);
            }

            sessionManager.CurrentUserToken = string.Empty;
            loginModel.UserIp = GetClientIPAddress();
            resource = "/api/av_sessions/login";
            UserModel data = new UserModel();
            LoginValidator loginValidator = new LoginValidator();
            var validator = loginValidator.Validate(loginModel);
            if (validator.Errors.Count() == 0)
            {
                data = await Post<UserModel>(loginModel);
                sessionManager.CurrentUserToken = data.Data.Token;
                //sessionManager.LoggedInUserName = loginModel.UserName;
                if (data.Errors.Count > 0)
                {
                    sessionManager.CurrentUserToken = string.Empty;
                    TempData["ErrorMessages"] = string.Join("<br>", data.Errors);
                    return View("SignIn");
                }
                else
                {
                    resource = "/api/applications";
                    var response = await Get<ApplicationListModel>();
                    this.sessionManager.UserName = loginModel.UserName;
                    if (!IsValidApps(response))
                    {
                        return RedirectToAction("WelCome", "Account");
                    }
                }
            }
            else
            {
                var errors = validator.Errors.Select(e => e.ErrorMessage).ToList();
                data.Status = Constants.Error;
                data.Errors = errors;
                sessionManager.CurrentUserToken = string.Empty;
                TempData["ErrorMessages"] = string.Join("<br>", data.Errors);
                return View("SignIn");
            }
        }
        if (actionType == "Register")
        {
            return View("Register");
        }
        return RedirectToAction("DashBoard", "Home");
    }
    public ActionResult SignOff()
    {
        //formsAuth.SignOut();
        //if (HttpContext.Session != null) HttpContext.Session.RemoveAll();           
        this.sessionManager.CurrentUserToken = string.Empty;
        return RedirectToAction("SignIn", "Account");
    }
  • We need to see your controller actions before we can tell you why you're experiencing what you are. – David L Jun 17 '15 at 05:01
  • David could you please check it, i have modified the question. – durga siva kishore mopuru Jun 17 '15 at 06:19
  • This is not the controller. This is just the filter. This still doesn't explain what they're getting redirected ***to***. – David L Jun 17 '15 at 13:22
  • @DavidL when i try to redirect, it is redirecting but it is rendering on UserLayout page and i want to render inside the LayoutPage of Login – durga siva kishore mopuru Jun 18 '15 at 04:25
  • One of the `View` overloads allows you to pass the layout path. This can also be set in the view itself. – ps2goat Jun 18 '15 at 04:48
  • possible duplicate of [How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?](http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file) – ps2goat Jun 18 '15 at 04:49
  • @ps2goat i also did this but the login layout is not rendering `return View("SignIn", "~/Views/Shared/_LoginLayout.cshtml");` – durga siva kishore mopuru Jun 18 '15 at 06:21
  • @ps2goat is their any alternative way to pass layout as a parameter from RedirectToRouteResult?? – durga siva kishore mopuru Jun 18 '15 at 06:50
  • The view should be passed in as `"_LoginLayout"`. MVC will already look for the view in the correct location, and the extension is not required; using the extension will actually throw an error. – ps2goat Jun 18 '15 at 14:26

0 Answers0