首页 新闻 会员 周边 捐助

MVC如何拿掉登陆页面 直接跳转到主页面

0
悬赏园豆:20 [已解决问题] 解决于 2014-03-21 10:59

我下载的一个模板,想把登陆页面拿掉,通过其他验证方法直接传递一个工号给Controller 中的Logon。但是不加载登陆页面就是无法跳转到 LogOn(LogonViewModel model, string returnUrl),代码如下,帮忙解决,谢谢~

 

程序第一次加载运行顺序:OnAuthorization→LogOn()

加载登陆页面输入账号以后的运行顺序:OnAuthorizationLogOn(LogonViewModel model, string returnUrl)

怎样让程序第一次加载的时候的运行顺序就是 OnAuthorization→LogOn(LogonViewModel model, string returnUrl) 

第一次的OnAuthorization 和加载登陆页面以后的OnAuthorization 运行的代码完全一样,为什么第一次不直接跳转到LogOn(LogonViewModel model, string returnUrl)

 

详细代码如下,谢谢~

 

AccountController 中的两个Logon Method

 public class AccountController : BaseController {

      [AllowAnonymous]
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult LogOn(LogonViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)     
            {
                //Login页面输入工号以后,GetUserByLoginID 从DB查询LoginUser的信息
                User UserInfo = GetUserByLoginID(model.UserName);
                if (UserInfo == null)
                {
                    //账号无登陆权限
                    //SetFlashMsg(ExceptionResx.exAccountLogon);
                    //return RedirectToAction("Index", "Home");
                }
                else if (UserInfo.UserEnable == false)
                {
                    //账号Disable

                    //SetFlashMsg(ExceptionResx.exAccountDisable);
                    //return RedirectToAction("Index", "Home");
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    FormsAuthenticationTicket ticket =
                        new FormsAuthenticationTicket(
                            1,
                            model.UserName,
                            DateTime.Now,
                            DateTime.Now.AddHours(2),
                            false,
                            UserInfo.UserId.ToString(),
                            FormsAuthentication.FormsCookiePath);
                    FormsAuthentication.RenewTicketIfOld(ticket);
                    string encryptTicket = FormsAuthentication.Encrypt(ticket);
                    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket));

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                       && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                        //return RedirectToAction("Logon", "Account");
                    }
                }
            }
            //return View(model);
            return RedirectToAction("Index", "Home");
        }

}
View Code

BaseController 中的 protected override void OnAuthorization

    protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                string CurrController = filterContext.RouteData.Values["controller"].ToString();
                string CurrAction = filterContext.RouteData.Values["action"].ToString();

                FormsIdentity id = (FormsIdentity)filterContext.HttpContext.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;

                if (!string.IsNullOrEmpty(ticket.UserData))
                {
                    //string userCacheKey = string.Format("ST_User_{1}_{0}", ticket.UserData.Split('|').First(), Session.SessionID);
                    string menuCacheKey = string.Format("ST_Menu_{1}_{0}", ticket.UserData.Split('|').First(), Session.SessionID);

                    _CurrentUser = Factory.UserRepository.GetUserByUserId(int.Parse(ticket.UserData));
                    ViewData["User"] = _CurrentUser.Name;


                    //AJAX not need Menu
                    if (Array.IndexOf(_arySkipController, CurrController.ToUpper()) == -1)
                    {
                        if (filterContext.HttpContext.Cache[menuCacheKey] == null
                                || Session["refresh_menu"] != null)
                        {
                            filterContext.HttpContext.Cache[menuCacheKey] = CaculateMenu(null);
                            Session.Remove("refresh_menu");
                        }
                        ViewData["Menu"] = filterContext.HttpContext.Cache[menuCacheKey] as IEnumerable<MenuItem>;

                        List<Function> Functions = Factory.UserRepository.GetFunctionsByUserId(_CurrentUser.UserId).ToList();

                        if (Session["Error"] != null)
                        {
                            Session["Error"] = null;
                            filterContext.Result = new HttpUnauthorizedResult();
                            return;
                        }

                        if (_CurrentUser.FirstLogin || _CurrentUser.Reset_Password_Due_Date <= DateTime.Now)
                        {
                            if (CurrController.ToUpper() == "ACCOUNT" &&
                                (CurrAction.ToUpper() == "CHANGEPASSWORD" || CurrAction.ToUpper() == "LOGOUT"))
                            {
                                return;
                            }
                            else
                            {
                                SetFlashMsg("you must change your password.");
                                filterContext.Result = RedirectToAction("ChangePassword", "Account");
                            }
                        }

                        if (
                            (CurrController.ToUpper() != "HOME" && CurrAction.ToUpper() != "INDEX")
                             && (CurrController.ToUpper() != "ACCOUNT" && (CurrAction.ToUpper() != "LOGON" || CurrAction.ToUpper() != "LOGOUT"))
                            )
                        {

                            List<Function> CheckEntity = Functions.Where(f => f.Controller == CurrController
                                                    && f.Action == CurrAction).ToList();

                            if (CheckEntity == null || CheckEntity.Count == 0)
                            {
                                Session["Error"] = "Y";
                                filterContext.Result = new HttpUnauthorizedResult();
                                return;
                            }
                        }
                    }
                }
            }
            ViewData["SystemVersion"] = "Version: " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
            base.OnAuthorization(filterContext);
        }
View Code
sirili的主页 sirili | 初学一级 | 园豆:44
提问于:2014-03-20 18:53
< >
分享
最佳答案
0

第一个不带参的logon()应该属于get请求,请求logon这个view视图,而后一个logon属于post请求,这是提交数据。

收获园豆:10
单恋 | 小虾三级 |园豆:678 | 2014-03-20 19:05

运行程序,就会调到第一个Get请求方法,然后加载登陆頁面,输入工號以后 才会加载第二个Logon方法

 

我想直接加载第二个Logon方法,该怎么弄?

sirili | 园豆:44 (初学一级) | 2014-03-20 21:18

@sirili: 你别把它想成两个方法嘛,你把第二个logon想成一个事件嘛。你都说了。要输入工号嘛,不要第一个,你在哪点输工号?

单恋 | 园豆:678 (小虾三级) | 2014-03-20 21:42
其他回答(2)
0

假设你的LogonViewModel里面有“工号”这个属性

 

Account/Logon?工号=xxx&url=xxx

这样就执行第二个action

收获园豆:5
ATATA | 园豆:5 (初学一级) | 2014-03-21 09:04
0

第二个logon,是必须提交才能访问的,想直接访问,没搞过

收获园豆:5
开山怪不怪 | 园豆:544 (小虾三级) | 2014-03-21 09:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册