Esempio n. 1
0
        /// <summary>
        /// 組織登入API
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="deviceId"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public async Task <bool> OrganizationLoginCheck(string loginUrl, LoginRequest data, int orgId)
        {
            var db              = _uow.DbContext;
            var response        = false;
            var loginColumnKeys = db.OrganizationLoginColumn.Where(t => t.OrgId == orgId);
            var checkColumn     = loginColumnKeys.FirstOrDefault();

            if (checkColumn == null)
            {
                return(response);
            }

            var contentJson       = string.Empty;
            var tokenInfo         = new Infrastructure.ViewModel.Base.BackendBaseRequest();
            var schoolLoginKey    = ConfigurationManager.AppSettings["iCanLoginKey"].ToString();
            var appLoginKey       = ConfigurationManager.AppSettings["AppLoginKey"].ToString();
            var encryptionService = new Encryption();
            var accounts          = data.Account.Split('@');
            var account           = accounts[0];
            var simulateAccount   = accounts.Count() > 1 ? accounts[1] : account;

            //有模擬登入者,需要驗證模擬登入者是否為系統管理者
            if (accounts.Count() > 1)
            {
                var checkSimulatorAuth = db.Members.FirstOrDefault(t => t.Account == account).RoleName == "1";
                if (checkSimulatorAuth == false)
                {
                    return(false);
                }
            }


            using (var httpClient = new HttpClient())
            {
                var password = encryptionService.EncryptString(encryptionService.DecryptString(data.Password, appLoginKey), schoolLoginKey);
                if (checkColumn.Method.ToLower() == "post")
                {
                    var jsonData = "{";
                    foreach (var column in loginColumnKeys)
                    {
                        switch (column.Type.ToLower())
                        {
                        case "account":
                            jsonData = string.Format("{0}\"{1}\":\"{2}\",", jsonData, column.ColumnKey, account);
                            break;

                        case "pwd":
                            jsonData = string.Format("{0}\"{1}\":\"{2}\",", jsonData, column.ColumnKey, password);
                            break;

                        case "devicekey":
                            jsonData = string.Format("{0}\"{1}\":\"{2}\",", jsonData, column.ColumnKey, data.PhoneID);
                            break;

                        case "simulate":
                            jsonData = string.Format("{0}\"{1}\":\"{2}\",", jsonData, column.ColumnKey, simulateAccount);
                            break;

                        default:
                            break;
                        }
                    }
                    jsonData = jsonData.Substring(0, jsonData.Length - 1);
                    httpClient.BaseAddress = new Uri(loginUrl);
                    jsonData += "}";
                    var request = new HttpRequestMessage(HttpMethod.Post, loginUrl);
                    request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
                    var responseContent = httpClient.SendAsync(request);
                    if (responseContent.Result.IsSuccessStatusCode)
                    {
                        var responseString = responseContent.Result.Content.ReadAsStringAsync().Result;
                        tokenInfo = JsonConvert.DeserializeObject <Infrastructure.ViewModel.Base.BackendBaseRequest>(responseString);
                    }
                }
                else if (checkColumn.Method.ToLower() == "get")
                {
                    loginUrl += "?";
                    foreach (var column in loginColumnKeys)
                    {
                        switch (column.Type.ToLower())
                        {
                        case "account":
                            loginUrl = string.Format("{0}{1}={2}&", loginUrl, column.ColumnKey, account);
                            break;

                        case "pwd":
                            loginUrl = string.Format("{0}{1}={2}&", loginUrl, column.ColumnKey, password);
                            break;

                        case "devicekey":
                            loginUrl = string.Format("{0}{1}={2}&", loginUrl, column.ColumnKey, data.PhoneID);
                            break;

                        case "simulate":
                            loginUrl = string.Format("{0}{1}={2}&", loginUrl, column.ColumnKey, simulateAccount);
                            break;

                        default:
                            break;
                        }
                    }
                    loginUrl = loginUrl.Substring(0, loginUrl.Length - 1);
                    var responseContent = await httpClient.GetAsync(loginUrl);

                    var responseString = responseContent.Content.ReadAsStringAsync().Result;
                    tokenInfo = JsonConvert.DeserializeObject <BackendBaseRequest>(responseString);
                }

                response = tokenInfo.Success;
                if (tokenInfo.Success)
                {
                    var tokenService    = new TokenService();
                    var memberService   = new MemberService();
                    var orgToken        = tokenInfo.Token != null && tokenInfo.Token != string.Empty ? tokenInfo.Token : Guid.NewGuid().ToString().ToLower();
                    var loginMemberInfo = data.OrgId.HasValue ?
                                          db.Members.FirstOrDefault(t => t.OrgId == data.OrgId.Value && t.Account == simulateAccount) :
                                          db.Members.FirstOrDefault(t => t.Account == simulateAccount);
                    var loginLogResponse = tokenService.InsertUserTokenByOrganization(data.RequestSystem, data.PhoneID, loginMemberInfo, data.PushToken, orgToken, loginMemberInfo.Id);
                }
            }
            return(response);
        }