コード例 #1
0
        public ActionResult Register(UserAccount model)
        {
            //验证
            if (ModelState.IsValid)
            {
                var user = new Common.Entities.AdminEntity {
                    Name = model.Email, Password = model.Password, AddDate = DateTime.Now
                };
                var result = AdminBLL.AddAccount(user);
                if (result > 0)
                {
                    var accountView = new UserData()
                    {
                        UserName = model.Email,
                        UserId   = result,
                        Roles    = new List <int>()
                        {
                            1, 2, 3
                        }
                    };
                    HttpFormsAuthentication.SetAuthenticationCoolie(accountView, 7);
                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // 发送包含此链接的电子邮件
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");

                    return(RedirectToAction("Index", "Home"));
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return(View(model));
        }
コード例 #2
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                int userId = AccountBLL.ValidateAccount(model.Email, model.Password);

                if (userId > 0)
                {
                    //验证成功,用户名密码正确,构造用户数据
                    var userData = new HttpUserDataPrincipal {
                        UserId = userId, UserName = model.Email
                    };

                    //保存Cookie
                    HttpFormsAuthentication <HttpUserDataPrincipal> .SetAuthCookie(model.Email, userData, model.RememberMe);

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "提供的用户名或密码不正确。");
                }
            }

            return(View(model));
        }
コード例 #3
0
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                int userId = AccountBLL.Add(model.Email, model.Password);

                if (userId > 0)
                {
                    //注册成功,用户名密码正确,构造用户数据
                    var userData = new HttpUserDataPrincipal {
                        UserId = userId, UserName = model.Email
                    };

                    //保存Cookie
                    HttpFormsAuthentication <HttpUserDataPrincipal> .SetAuthCookie(model.Email, userData, false);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "用户名已存在。");
                }
            }

            return(View(model));
        }
コード例 #4
0
        public ActionResult Login()
        {
            string userName     = HttpContext.Request["userName"];
            string pwd          = HttpContext.Request["PWD"];
            string rememberDays = HttpContext.Request["rememberDays"];

            if (userName == "4545" && pwd == "123")
            {
                UserData ud = new UserData();
                ud.UserName = "******";
                ud.UserId   = "78787";
                ud.Phone    = "18611347259";
                HttpFormsAuthentication.SetAuthenticationCookie("刘飞", ud, 5);
            }
            else
            {
            }
            return(new RedirectResult("/home/index"));
        }
コード例 #5
0
        public ActionResult Index(string name, string password, bool rememberMe, string returnUrl)
        {
            if (string.IsNullOrEmpty(name))
            {
                ViewBag.ErrorMsg = "用户名不能为空!";
                return(View());
            }
            var account = AdminBLL.GetAccountByName(name);

            if (account == null)
            {
                ViewBag.ErrorMsg = "用户不存在!";
                return(View());
            }
            if (account.Password != password)
            {
                ViewBag.ErrorMsg = "密码不正确!";
                return(View());
            }
            var accountView = new UserData()
            {
                UserName = account.Name,
                UserId   = account.ID,
                Roles    = new List <int>()
                {
                    1, 2, 3
                }
            };

            HttpFormsAuthentication.SetAuthenticationCoolie(accountView, rememberMe ? 7 : 0);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return(Redirect(returnUrl));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
コード例 #6
0
ファイル: Global.asax.cs プロジェクト: dylanpeng/ACEAdmin
 protected void Application_PostAuthenticateRequest(object sender, System.EventArgs e)
 {
     HttpContext.Current.User =
         HttpFormsAuthentication <HttpUserDataPrincipal> .TryParsePrincipal(HttpContext.Current.Request);
 }
コード例 #7
0
 //从Cookie中解析票据信息
 void Application_PostAuthenticateRequest(object sender, EventArgs e)
 {
     HttpContext.Current.User = HttpFormsAuthentication.TryParsePrincipal <UserData>(HttpContext.Current);
 }