当session id null使用angularjs或mvc重定向到登录页面时

时间:2015-06-17 04:54:12

标签: c# asp.net .net asp.net-mvc angularjs

如果CurretUserToken为null我正在重定向到LoginPage,但它在当前布局上重定向,我想在LoginLayout页面上呈现

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" }));
    }

这是我的控制器,当CurrentUserToken为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");
    }

0 个答案:

没有答案