Пример #1
0
        public ActionResult Login(LoginViewModel model)
        {
           // FormsAuthentication.SetAuthCookie(model.Email, false, FormsAuthentication.FormsCookiePath);
           // HttpContext.User.Identity.Name = "sdf";
       
          
            MyPrincipal principal = new MyPrincipal(model.Email,model.Password);
            if (!principal.Identity.IsAuthenticated)
            {
               
            }
            else
            {
                FormsAuthentication.SetAuthCookie(model.Email, false, FormsAuthentication.FormsCookiePath);
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(model.Email, false, 5);

                FormsIdentity identy = new FormsIdentity(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                Response.Cookies.Add(cookie);
                //User.Identity.
                System.Web.HttpContext.Current.User = principal;
          
                // 如果用户通过验证,则将用户信息保存在缓存中,以备后用 
                // 在实际中,朋友们可以尝试使用用户验证票的方式来保存用户信息,这也是.NET内置的用户处理机制 
               // HttpContext.GetOwinContext().Authentication.
               // var Muser = User as principal;
              //  User = principal;
                Hashtable userMessage = new Hashtable();
                userMessage.Add("UserID", model.Email);
                userMessage.Add("UserPassword", model.Password);
                //Cache CA = new Cache();
                //CA.Insert("UserMessage", userMessage);

                System.Web.HttpContext.Current.Cache.Insert("UserMessage", userMessage);
                //System.Web.HttpContext.Current.Cache.Insert()
                
            //  Cache.Insert("UserMessage", userMessage); 
                
              
            } 
           // HttpContext.GetOwinContext().Authentication.User.Identity.IsAuthenticated
           // User.Identity.u
            UserB.InsertUser(new User { Name = "shens" });
          
            //if (!ModelState.IsValid)
            //{
            //    return View();
            //}
            return View("XuLogin");
          //  this.RedirectToAction("xulogin","account")
           
        }
Пример #2
0
            /// <summary>  

            /// 在过程请求授权时调用。   

            /// </summary>  

            /// <param name="filterContext">对象包括控制器、HTTP 上下文、请求上下文、操作结果和路由数据。</param>  

            public override void OnAuthorization(AuthorizationContext filterContext)
            {
                var dd = filterContext.HttpContext.Request.Form["Email"];
                // var s=  System.Web.Caching.Cache[];
                string sds = this.Roles;
                var cocache = filterContext.HttpContext.Cache["UserMessage"];
                //Hashtable userMessage = new Hashtable();
                //userMessage.Add("UserID", model.Email);
                //userMessage.Add("UserPassword", model.Email);

                Hashtable hasht = cocache as Hashtable;
              string username=  hasht["UserID"].ToString();
              string userpwd = hasht["UserPassword"].ToString();
               // bool IsA = HttpContext.Current.User.Identity.IsAuthenticated;
                if (filterContext.ActionDescriptor.ActionName.ToLower() == "login")
                { }
                else
                {

                    
                        //这一步很重要,要代替.NET的自身的User.  

                        // IPrincipal
                        MyPrincipal MyPrincipal = new MyPrincipal(username,userpwd);
                    bool sd=    MyPrincipal.Identity.IsAuthenticated;
                        HttpContext.Current.User = MyPrincipal;

                        if (HttpContext.Current.User.Identity.IsAuthenticated)
                            return;
                        else {
                            FormsAuthentication.SignOut();
                            filterContext.Result = new RedirectResult("/account/Login");
                     
                        }




                    

                   

                      
                    

                }
            }