예제 #1
0
파일: Global.asax.cs 프로젝트: c-tn/SNW
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            if (FormsAuthentication.CookiesSupported == true)
            {
                if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
                {
                    try
                    {
                        //let us take out the username now                
                        string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                        string roles = string.Empty;

                        using (DataContext db = new DataContext())
                        {
                            User user = db.Users.SingleOrDefault(u => u.Login == username);

                            roles = user.Roles;
                        }
                        //let us extract the roles from our own custom cookie


                        //Let us set the Pricipal with our user specific details
                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
                          new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
                    }
                    catch (Exception)
                    {
                        //somehting went wrong
                    }
                }
            }
        } 
        public ActionResult LoginView(LoginClass model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                using (DataContext db = new DataContext())
                {
                    string userEmail = model.Login;
                    string Password = model.Password;
                    bool userValid = db.Users.Any(u => u.Login == userEmail && u.Password == Password);
                    if (userValid)
                    {
                        FormsAuthentication.SetAuthCookie(userEmail, false);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("PersonalOffice", "Account",model);
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Пользователя с таким логином и паролем нет");
                    }
                }
            }

            return View(model);
        }
예제 #3
0
        public ActionResult RegisterView(RegisterationClass model)
        {
            string pass = model.Password;
            EncryPass encpass = new EncryPass();
            pass= encpass.GetHashString(pass);
            if (ModelState.IsValid)
            {
                User user = null;
                using (DataContext db = new DataContext())
                {
                    
                    user = db.Users.FirstOrDefault(u => u.Login == model.Login);
                }
                if (user == null)
                {
                    db.Users.Add(new User { Login = model.Login, Password = pass });
                    db.SaveChanges();
                    user = db.Users.Where(u => u.Login == model.Login && u.Password == pass).FirstOrDefault();
                    id = user.Id;
                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(model.Login, true);

                        return RedirectToAction("PersonalOffice", "Account");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Пользователь с таким логином уже существует");
                }
            }
            return View(model);
        }