示例#1
0
        public async Task <List <ModuleApplication> > Initial(string group)
        {
            var data = new List <ModuleApplication>();
            var principalIdentity = RequestContext.Principal.Identity;
            var userId            = principalIdentity.GetUserId();

            //1 - CMS, 2 - Patient Services, 3 - Internal Services
            if (userId.Length > 0)
            {
                string key = ":" + group + ":" + userId;
                data = (List <ModuleApplication>)MemoryCacheObject.GetCacheObject(ObjectCacheProfile.CACHE_MODULE_USER + key);
                if (data == null)
                {
                    List <string> list = new List <string> {
                        "Group", "Userid", "DefaultRoleInitial"
                    };
                    var para = APIProvider.APIGeneratorParameter(list, group, userId, APIConstant.DefaultRoleInitial);

                    var source = await _module.Initial(para);

                    data = Mapper.Map <List <ModuleApplication> >(source);

                    MemoryCacheObject.CacheObject(ObjectCacheProfile.CACHE_MODULE_USER + key, data, 1200); //6h
                }

                if (data == null)
                {
                    data = new List <ModuleApplication>();
                }

                return(data);
            }
            return(data);
        }
示例#2
0
        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);
        }
示例#3
0
        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));
        }