Example #1
0
        public static String GetUserCode(byte[] privateKey)
        {
            //create new otp auth with user private key
            GoogleTOTP googleTOTP = new GoogleTOTP(privateKey);

            //get otp auth
            return(googleTOTP.GeneratePin());
        }
 public override void ViewDidLoad()
 {
     if (Application.ActualUserPrivateKey == null)
     {
         googleTOTP = new GoogleTOTP();
         btnDesactivateDAuth.Enabled = false;
         btnActivateDAuth.Enabled    = true;
     }
     else
     {
         googleTOTP = new GoogleTOTP(Application.ActualUserPrivateKey);
         btnDesactivateDAuth.Enabled = true;
         btnActivateDAuth.Enabled    = false;
     }
 }
Example #3
0
        public ActionResult GenerateOTP(string data)
        {
            var dataDecrypt = JsonConvert.DeserializeObject <dynamic>(data.Decrypt());
            var username    = (string)dataDecrypt.UserName;
            var secretKey   = (string)dataDecrypt.SecretKey;
            var expiredTime = (System.DateTime)dataDecrypt.ExpiredTime;
            //Sinh ra ảnh QR code
            var googleOPTAuthenticator = new GoogleTOTP();
            var qRCodeImage            = googleOPTAuthenticator.GenerateImage(secretKey, "SSO.GLOBAL-" + username);

            string result = "";

            //var retVal = new OTPManagerModel();
            if (expiredTime > System.DateTime.Now)
            {
                result = qRCodeImage;
            }
            else
            {
                result = "The QRCode is expired.Please contact IT to be supported.";
            }

            return(View("AllocateOTP", null, result));
        }
Example #4
0
        public JsonResult Login(SystemUserLoginItemExtend user, string returnUrl)
        {
            try
            {
                if (FormsAuthentication.CookiesSupported)
                {
                    if (ModelState.IsValid)
                    {
                        user.UserName = user.UserName.ToLower();
                        var error = SystemAuthenticate.Login(user);
                        if (error == SystemCommon.Error.LoginSuccess)
                        {
                            //Check OTP
                            var userInfo  = userDA.GetListByUsername(user.UserName).FirstOrDefault();
                            var secretkey = ConfigurationManager.AppSettings["OTPSecretKey"] + userInfo.OtpPrivateKey;
                            if (!GoogleTOTP.IsVaLid(secretkey, user.OTP))
                            {
                                objMsg.Error = true;
                                objMsg.Title = getMessageError(SystemCommon.Error.InfoIncorrect);
                                //xóa cookie (check OTP phải xử lý sau khi login, vì login xử lý ở dll,nên nếu OTP ko chính xác thì phải xóa cookie
                                FormsAuthentication.SignOut();
                                return(Json(objMsg));
                            }

                            ////Cập nhật trường IsLogin trong bảng Userclient
                            var userClientDa  = new UserClientDA();
                            var lstUserClient = userClientDa.GetListByUsername(user.UserName).ToList();
                            if (lstUserClient.Count(m => m.IsLogin) == 0)
                            {
                                foreach (var item in lstUserClient)
                                {
                                    item.IsLogin = true;
                                    userClientDa.Update(item);
                                }
                            }

                            if (string.IsNullOrEmpty(returnUrl))
                            {
                                objMsg.Title = ConfigurationManager.AppSettings["DefaultReturnUrl"];
                            }
                            else
                            {
                                objMsg.Title = returnUrl;
                            }
                        }
                        else
                        {
                            objMsg.Title = getMessageError(error);
                            objMsg.Error = true;
                        }
                    }
                    else
                    {
                        objMsg.Error = true;
                        objMsg.Title = string.Join("</br>", ModelState.Keys.SelectMany(k => ModelState[k].Errors).Select(m => m.ErrorMessage));
                    }
                }
            }
            catch (Exception ex)
            {
                objMsg.Error = true;
                objMsg.Title = ex.Message;
            }

            return(Json(objMsg));
        }
Example #5
0
        public JsonResult Logincallback(SystemUserLoginItemExtend user)
        {
            try {
                //Nếu như user chưa đăng nhập bất cứ phần mềm nào = đã logout tất cả phần mềm và SSO đã bị logout thì bật tất cả các
                var flag = (User == null);//SSO bị logout
                if (FormsAuthentication.CookiesSupported)
                {
                    if (ModelState.IsValid)
                    {
                        if (user.SSOType == "Global")
                        {
                            user.UserName = user.UserName.ToLower();
                            var error = SystemAuthenticate.Login(user);
                            if (error == SystemCommon.Error.LoginSuccess)
                            {
                                //Check OTP
                                var userInfo  = userDA.GetListByUsername(user.UserName).FirstOrDefault();
                                var secretkey = ConfigurationManager.AppSettings["OTPSecretKey"] + userInfo.OtpPrivateKey;
                                if (!GoogleTOTP.IsVaLid(secretkey, user.OTP))
                                {
                                    objMsg.Error = true;
                                    objMsg.Title = getMessageError(SystemCommon.Error.InfoIncorrect);
                                    //xóa cookie (check OTP phải xử lý sau khi login, vì login xử lý ở dll,nên nếu OTP ko chính xác thì phải xóa cookie
                                    FormsAuthentication.SignOut();
                                    return(Json(objMsg));
                                }

                                var userClientDA = new UserClientDA();
                                if (flag) //(*)
                                {
                                    var lstUserClient = userClientDA.GetListByUsername(user.UserName);
                                    if (lstUserClient.Count(m => m.IsLogin) == 0)
                                    {
                                        foreach (var item in lstUserClient)
                                        {
                                            item.IsLogin = true;
                                            userClientDA.Update(item);
                                        }
                                    }
                                }
                                else //(**)
                                {
                                    var clientDA   = new ClientDA();
                                    var domain     = GetDomain(user.ReturnUrl);
                                    var client     = clientDA.GetByDomain(domain);
                                    var userClient = userClientDA.GetListByUsernameAndClientId(user.UserName, client.Id);
                                    if (userClient != null)
                                    {
                                        userClient.IsLogin = true;
                                        userClientDA.Update(userClient);
                                    }
                                }

                                if (string.IsNullOrEmpty(user.ReturnUrl))
                                {
                                    objMsg.Title = ConfigurationManager.AppSettings["DefaultReturnUrl"];
                                }
                                else
                                {
                                    objMsg.Title = string.Format("{0}{1}{2}", user.ReturnUrl, "?data=", HttpUtility.UrlEncode(GetReturnData(user)));
                                }
                            }
                            else
                            {
                                objMsg.Title = getMessageError(error);
                                objMsg.Error = true;
                            }
                        }
                        else
                        {
                            var url = ConfigurationManager.AppSettings["SSOVN"];
                            user.SecretKey = Security.CreateKey();
                            var ssoVNResponse = JsonConvert.DeserializeObject <Message>(HttpUtils.MakePostRequest(url, JsonConvert.SerializeObject(user), "application/json"));
                            objMsg = ssoVNResponse;
                        }
                    }
                    else
                    {
                        objMsg.Error = true;
                        objMsg.Title = string.Join("</br>", ModelState.Keys.SelectMany(k => ModelState[k].Errors).Select(m => m.ErrorMessage));
                    }
                }
            }
            catch (Exception ex)
            {
                objMsg.Error = true;
                objMsg.Title = ex.Message;
            }
            return(Json(objMsg));
        }