public async Task <ActionResult> PersonalInfo(UserInfoBindingModel model, HttpPostedFileBase fileUpload) { controllerName = this.ControllerContext.RouteData.Values["controller"].ToString(); string strUrl = controllerName + ConstantDomain.UPDATE_USER; var token = _userSession.BearerToken; if (ModelState.IsValid) { try { byte[] image = null; MemoryStream target = new MemoryStream(); if (fileUpload != null) { fileUpload.InputStream.CopyTo(target); image = target.ToArray(); cacheUserData = cacheUserData == null?AccessCacheUserData() : cacheUserData; cacheUserData.Image = FileManagement.ByteArrayToImageBase64(image); model.Image = image; } var result = await APIProvider.Authorize_DynamicTransaction <UserInfoBindingModel, string>(model, token, strUrl, APIConstant.API_Resource_Authorize, ARS.IgnoredARS); if (Response.StatusCode == 200) { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.SUCCESS, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_UPDATE, ApplicationGenerator.TypeResult.SUCCESS)); } else { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_UPDATE, ApplicationGenerator.TypeResult.FAIL)); } return(RedirectToAction("Overview")); } catch (Exception ex) { Logger.LogError(ex); TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.ERROR, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_UPDATE, ApplicationGenerator.TypeResult.ERROR)); throw ex; } } else { StringBuilder errorBuilder = new StringBuilder(); foreach (var modelError in ModelState) { if (modelError.Value.Errors.Count > 0) { errorBuilder.AppendLine(modelError.Value.Errors[0].ErrorMessage.ToString()); } } TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, errorBuilder.ToString()); return(View(model)); } }
private UserCache AccessCacheUserData() { var userCache = (UserCache)MemoryCacheObject.GetCacheObject(ObjectCacheProfile.CACHE_PROFILE_USER + _userSession.UserId); if (userCache == null) { UserSecretInfoViewModel info = APIProvider.Authorize_GetNonAsync <UserSecretInfoViewModel>(_userSession.BearerToken, "Account", "GetSecretInfo", null, APIConstant.API_Resource_Authorize); if (info != null) { var patientId = (info.PatientId == null ? string.Empty : info.PatientId); UserCache cacheUserData = new UserCache(); cacheUserData.Image = (info.Image != null ? FileManagement.ByteArrayToImageBase64(info.Image) : string.Empty); cacheUserData.UserName = _userSession.UserName; cacheUserData.PatientId = patientId; cacheUserData.UserId = _userSession.UserId; MemoryCacheObject.CacheObject(ObjectCacheProfile.CACHE_PROFILE_USER + _userSession.UserId, cacheUserData); return(cacheUserData); } } return(userCache); }
public async Task <ActionResult> Login(LoginModel model, string returnUrl) { //Check Captcha if (GlobalVar.IsreCaptcha) { var response = Request["g-recaptcha-response"]; var client = new WebClient(); var result = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", ValueConstant.GooglereCAPTCHA_SecretKey, response)); var obj = JObject.Parse(result); var status = (bool)obj.SelectToken("success"); if (!status) { ModelState.AddModelError(string.Empty, ""); TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.ERROR, ApplicationGenerator.GeneralActionMessage(null, ApplicationGenerator.TypeResult.reCAPTCHA)); return(View(model)); } } var token = AuthenAPIHelper.GetToken(model.UserName, model.Password); if (string.IsNullOrEmpty(token.AccessToken)) { var errorStr = "Có lỗi phát sinh khi đăng nhập: Không lấy được Token, kiểm tra tài khoản + password."; if (token.Json != null) { var error = JsonConvert.DeserializeObject <dynamic>(token.Json.ToString()); errorStr = error.error_description.ToString(); } ModelState.AddModelError(string.Empty, errorStr); TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.ERROR, ApplicationGenerator.GeneralActionMessage(null, ApplicationGenerator.TypeResult.LOGINERROR)); Logger.LogError(new Exception(errorStr)); return(View(model)); } var tokenDynamic = JsonConvert.DeserializeObject <dynamic>(token.Json.ToString()); string username = tokenDynamic.userName; string access_token = tokenDynamic.access_token; //Get Secret User Info UserSecretInfoViewModel info = await APIProvider.Authorize_Get <UserSecretInfoViewModel>(access_token, controllerName, "GetSecretInfo", null, APIConstant.API_Resource_Authorize); if (info == null) { ModelState.AddModelError(string.Empty, "Không tìm thấy thông tin tài khoản"); TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.ERROR, ApplicationGenerator.GeneralActionMessage(null, ApplicationGenerator.TypeResult.USER_NOT_EXIST)); return(View(model)); } var patientId = (info.PatientId == null ? string.Empty : info.PatientId); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, info.NameIdentifier, ClaimValueTypes.String), new Claim(ClaimTypes.Email, info.Email, ClaimValueTypes.String), new Claim(ClaimTypes.Name, info.Name, ClaimValueTypes.String), new Claim(ValueConstant.AccountName, username, ClaimValueTypes.String), //new Claim(ValueConstant.AccountImage, info.Image, ClaimValueTypes.), new Claim(ValueConstant.AccountPatient, (info.PatientId == null ? string.Empty : info.PatientId), ClaimValueTypes.String), new Claim(ValueConstant.TOKEN, string.Format("{0}", access_token), ClaimValueTypes.String) }; UserCache cacheUserData = new UserCache(); cacheUserData.Image = (info.Image != null ? FileManagement.ByteArrayToImageBase64(info.Image) : string.Empty); cacheUserData.UserName = username; cacheUserData.PatientId = patientId; cacheUserData.UserId = info.NameIdentifier; MemoryCacheObject.CacheObject(ObjectCacheProfile.CACHE_PROFILE_USER + info.NameIdentifier, cacheUserData); var claimsIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, claimsIdentity); HttpContext.User = AuthenticationManager.AuthenticationResponseGrant.Principal; return(RedirectToAction(returnUrl)); }
public async Task <ActionResult> Create(RegisterBindingModel model, HttpPostedFileBase fileUpload) { controllerName = this.ControllerContext.RouteData.Values["controller"].ToString(); string strUrl = controllerName + ConstantDomain.REGISTER; var token = _userSession.BearerToken; if (ModelState.IsValid) { //Call API Provider try { byte[] image = null; MemoryStream target = new MemoryStream(); if (fileUpload != null) { fileUpload.InputStream.CopyTo(target); image = target.ToArray(); cacheUserData = cacheUserData == null?AccessCacheUserData() : cacheUserData; cacheUserData.Image = FileManagement.ByteArrayToImageBase64(image); model.Image = image; } //else //{ // name = ConstValInternal.PATH_IMAGE_DEFAULT; //} //model.Image = name; var result = await APIProvider.Authorize_DynamicTransaction <RegisterBindingModel, string>(model, token, strUrl, APIConstant.API_Resource_Authorize, ARS.IgnoredARS); if (Response.StatusCode == 200) { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.SUCCESS, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_INSERT, ApplicationGenerator.TypeResult.SUCCESS)); return(RedirectToAction("Index")); } else { if (result.Contains(UserStatus.IsUsed.ToString())) { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_INSERT, ApplicationGenerator.TypeResult.USER_EXIST)); var listRole = await APIProvider.Authorize_Get <List <RoleViewModel> >(token, ConstantDomain.GET_ROLE, APIConstant.API_Resource_Authorize, ARS.IgnoredARS); if (listRole != null) { ViewBag.Role = listRole; } else { ViewBag.Role = new List <RoleViewModel>(); } return(View()); } else { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, ApplicationGenerator.GeneralActionMessage(APIConstant.ACTION_INSERT, ApplicationGenerator.TypeResult.FAIL)); var listRole = await APIProvider.Authorize_Get <List <RoleViewModel> >(token, ConstantDomain.GET_ROLE, APIConstant.API_Resource_Authorize, ARS.IgnoredARS); if (listRole != null) { ViewBag.Role = listRole; } else { ViewBag.Role = new List <RoleViewModel>(); } return(View()); } } } catch (HttpException ex) { TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.FuntionType.Account, APIConstant.ACTION_INSERT); return(RedirectToAction("Index")); } catch (Exception ex) { Logger.LogError(ex); throw ex; } } else { StringBuilder errorBuilder = new StringBuilder(); foreach (var modelError in ModelState) { if (modelError.Value.Errors.Count > 0) { errorBuilder.AppendLine(modelError.Value.Errors[0].ErrorMessage.ToString()); } } var listRole = await APIProvider.Authorize_Get <List <RoleViewModel> >(token, ConstantDomain.GET_ROLE, APIConstant.API_Resource_Authorize, ARS.IgnoredARS); if (listRole != null) { ViewBag.Role = listRole; } else { ViewBag.Role = new List <RoleViewModel>(); } TempData["Alert"] = ApplicationGenerator.RenderResult(ApplicationGenerator.TypeResult.FAIL, errorBuilder.ToString()); return(View(model)); } }