Example #1
0
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                BonaStocoWSLogon bonaWSLogon = new BonaStocoWSLogon();
                bonaWSLogon.Login(model.UserName, model.Password);
                bool authenticateOnInternet = bonaWSLogon.IsAuthenticated;
                bool validatedUserOnLocal = Membership.ValidateUser(model.UserName, model.Password);

                if (authenticateOnInternet && !validatedUserOnLocal)
                {
                    try
                    {
                        Membership.DeleteUser(model.UserName);
                        bonaWSLogon.CreateLocalUserIfNecessary();
                        validatedUserOnLocal = true;
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", "Gagal memperbaharui password Anda di lokal dengan pesan error :" + e.GetInnermostException());
                    }
                }

                if (authenticateOnInternet)
                {
                    if (validatedUserOnLocal)
                    {
                        CompanyProfiles cp = new CompanyProfiles(this.HttpContext);
                        cp.CompanyReserved = bonaWSLogon.Response.reserved;
                        cp.CompanyId = bonaWSLogon.Response.companyid;
                        cp.CompanyName = bonaWSLogon.Response.company;
                        cp.Role = bonaWSLogon.ROLE;
                        cp.RoleName = APRoles.MapRoleName(cp.Role);
                        cp.HomePage = APRoles.MapHomePage(cp.Role);

                        Response.Cookies.Add(new HttpCookie("tenantid", cp.CompanyId.ToString()));
                        Response.Cookies.Add(new HttpCookie("tenantname", cp.CompanyName));

                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            UserToken = bonaWSLogon.Response.token;
                            return RedirectToAction(cp.HomePage, "Home");
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("",
                        bonaWSLogon.ErrorMessage.Trim() != string.Empty ?
                        bonaWSLogon.ErrorMessage :
                        "User atau password anda salah.");
                }
            }
            return View(model);
        }
Example #2
0
 public ActionResult UbahPassword(ChangePasswordModel model)
 {
     sercurityws.BonastocoServices bonaWS = new sercurityws.BonastocoServices();
     BonaStocoWSLogon wsLogon = new BonaStocoWSLogon();
     sercurityws.serverResponse response = bonaWS.changepassword(new sercurityws.changepasswd() { token = UserToken, oldpassword = wsLogon.MD5(model.OldPassword), newpassword = wsLogon.MD5(model.NewPassword) });
     if (response.status != 0)
     {
         ModelState.AddModelError("", response.message);
         return View();
     }
     MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
     bool changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
     if (!changePasswordSucceeded)
     {
         ModelState.AddModelError("", "Batal merubah password");
         return View();
     }
     FormsAuthentication.SignOut();
     return RedirectToAction("LogOn");
 }