예제 #1
0
        /// <summary>
        /// Will authenticate the user and save all banks associated with the institution
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="intitution"></param>
        /// <param name="hookUrl"></param>
        /// <returns></returns>
        public string AuthenticateBankUser(BankDataServiceOptions options, out List<BankLoginAccount> bankList)
        {
            var request = new RestRequest("connect", Method.POST);
            var body = new {
                client_id = ConfigurationManager.AppSettings["PlaidID"],
                secret = ConfigurationManager.AppSettings["PlaidSecret"],
                username = options.UserName,
                password = options.Password,
                type = options.Institution,
                options = new {
                    webhook = options.HookUrl,
                    login_only = true
                }
            };
            request.AddJsonBody(body);
            request.RequestFormat = DataFormat.Json;
            var response = Client.Execute(request);

            JsonObject jresponse = JsonConvert.DeserializeObject<JsonObject>(response.Content);

            JsonObject[] banks = JsonConvert.DeserializeObject<JsonObject[]>(jresponse["accounts"].ToString());
            bankList = banks.Select(bank => {
                var meta = JsonConvert.DeserializeObject<JsonObject>(bank["meta"].ToString());
                return new BankLoginAccount() {
                    LoginId = options.LoginId,
                    Name = meta["name"].ToString(),
                    ServiceId = bank["_id"].ToString()
                };
            }).ToList();

            if (response.StatusCode == HttpStatusCode.OK) {
                return jresponse["access_token"].ToString();
            } else {
                throw new Exception(response.StatusDescription);
            }
        }
예제 #2
0
        public ActionResult ConnectLogin(ConnectLoginViewModel model)
        {
            model.Login = Service.Logins.GetById(model.LoginId);
            var hookUrl = string.Format("{0}?loginId={1}", BuildHookUrl(), model.Login.Id);
            var options = new BankDataServiceOptions(model.Username, model.Password, model.Login.Institution, hookUrl,
                model.LoginId);
            var bankList = new List<BankLoginAccount>();
            var token = BankService.AuthenticateBankUser(options, out bankList);
            model.Login.AccessToken = token;
            Service.Logins.Upsert(model.Login);

            // Save banks
            foreach (BankLoginAccount bank in bankList) {
                Service.Accounts.Upsert(bank);
            }

            return RedirectToAction("AccountList", new {model.LoginId});
        }