Ejemplo n.º 1
0
        public Webzalo()
        {
            InitializeComponent();


            Zalo3rdAppInfo   appInfo   = new Zalo3rdAppInfo(2683610687619458655, "ONr32QXPrsW53C4eD5dA", "https://youruri.com");
            Zalo3rdAppClient appClient = new Zalo3rdAppClient(appInfo);
            string           loginUrl  = appClient.getLoginUrl();
            string           code      = "";
            JObject          token     = appClient.getAccessToken(code);

            //zaloweb.Source =;
        }
        public async Task <IActionResult> ZaloCallback(string code, string state)
        {
            Zalo3rdAppInfo   appInfo   = new Zalo3rdAppInfo(Int64.Parse(_externalProvider.Value.Zalo.AppId), _externalProvider.Value.Zalo.SecretCode, _externalProvider.Value.Zalo.CallbackUrl);
            Zalo3rdAppClient appClient = new Zalo3rdAppClient(appInfo);
            JObject          token     = appClient.getAccessToken(code);

            var accessToken = token["access_token"].ToString();

            JObject profile = appClient.getProfile(accessToken, "name, id, picture");

            var tmpData = profile.ToObject <ExternalUserModel>();
            var user    = await _userStoreExtend.FindByNameAsync("Zalo" + tmpData.id);

            if (user != null)
            {
                #region Get user zalo
                var permissions = await _roleStoreExtend.ReadByUser(user.Id);

                var userInfo = _mapper.Map <UserInfo>(user);
                userInfo.Permissions = permissions;
                string[] output = userInfo.FullName.Split(' ');
                foreach (string s in output)
                {
                    userInfo.LetterAvatar += s[0];
                }
                userInfo.Avatar       = (userInfo.Avatar == null ? "" : _imagePath.Value.URL + userInfo.Avatar);
                userInfo.PhoneNumber  = (userInfo.PhoneNumber == null ? "" : userInfo.PhoneNumber);
                userInfo.LetterAvatar = userInfo.LetterAvatar.ToUpper();
                #endregion

                var responseLogin = new ResponseNotification();
                var response      = new Response();
                responseLogin.type     = 1;
                response.response      = userInfo;
                responseLogin.response = response;
                //await PushNotification(state, "Okaylah", "Đăng nhập zalo thành công", "Success", JsonConvert.SerializeObject(responseLogin));
            }
            else
            {
                var responseLogin = new ResponseNotification();
                responseLogin.type = 1;
                var responseEr = new ResponseError();
                responseEr.status      = "404";
                responseEr.response    = accessToken;
                responseLogin.response = responseEr;
                //await PushNotification(state, "Okaylah", "Đăng nhập zalo thất công", "Register", JsonConvert.SerializeObject(responseLogin));
            }
            return(Ok());
        }
        public async Task <IActionResult> ZaloRegister([FromForm] ExternalUserLogin obj)
        {
            Zalo3rdAppInfo   appInfo   = new Zalo3rdAppInfo(Int64.Parse(_externalProvider.Value.Zalo.AppId), _externalProvider.Value.Zalo.SecretCode, _externalProvider.Value.Zalo.CallbackUrl);
            Zalo3rdAppClient appClient = new Zalo3rdAppClient(appInfo);

            JObject profile = appClient.getProfile(obj.AccessToken, "name, id, picture");
            var     tmpData = profile.ToObject <ExternalUserModel>();
            var     user    = new ApplicationUser();

            user.Email        = obj.Email;
            user.UserName     = "******" + tmpData.id;
            user.FullName     = tmpData.name;
            user.PasswordHash = tmpData.id.ToString();
            user.Avatar       = tmpData.picture.data.url;
            return(await ExternalUser(user));
        }
Ejemplo n.º 4
0
        public ZaloUtils()
        {
            var info = new Zalo3rdAppInfo(long.Parse(ZALO_APP_ID), ZALO_APP_SECRET, ZALO_APP_CALLBACK);

            appClient = new Zalo3rdAppClient(info);
        }
Ejemplo n.º 5
0
        public ApiResponse UpsertPersonalZalo()
        {
            ApiResponse response = new ApiResponse();

            response.msg = "Add channel error";
            try
            {
                var id_zalo_appid       = Request.Cookies["id_zalo_appid"];
                var id_zalo_channelid   = Request.Cookies["id_zalo_channelid"];
                var id_zalo_business_id = Request.Cookies["id_zalo_business_id"];
                var id_zalo_appsecret   = Request.Cookies["id_zalo_appsecret"];
                var id_zalo_name        = Request.Cookies["id_zalo_name"];
                var id_zalo_templateid  = Request.Cookies["id_zalo_templateid"];
                var codeZalo            = Request.Cookies["codeZalo"];

                if (string.IsNullOrWhiteSpace(codeZalo) || string.IsNullOrWhiteSpace(id_zalo_business_id))
                {
                    return(response);
                }

                var     appInfo   = new Zalo3rdAppInfo(Convert.ToInt64(id_zalo_appid), id_zalo_appsecret, _appSettings.BaseUrls.Web + "zaloauth");
                var     appClient = new Zalo3rdAppClient(appInfo);
                JObject tokenObj  = appClient.getAccessToken(codeZalo);

                var token = (string)tokenObj["access_token"];
                if (token == null)
                {
                    return(response);
                }
                var profile    = appClient.getProfile(token, "id,name,birthday,gender,picture");
                var profile_id = (string)profile["id"];
                if (!string.IsNullOrWhiteSpace(profile_id))
                {
                    // upsert
                    Channel para = new Channel();
                    para.business_id = id_zalo_business_id;
                    para.ext_id      = id_zalo_appid;
                    para.name        = id_zalo_name;
                    para.id          = id_zalo_channelid;
                    para.template_id = id_zalo_templateid;
                    para.token       = token;
                    para.type        = "zalo_personal";
                    para.secret      = id_zalo_appsecret;

                    var rs1 = Core.Helpers.WebHelper.HttpPostAsync(_appSettings.BaseUrls.Api + "brands/zalos/upsert", para).Result;
                    if (!rs1)
                    {
                        return(response);
                    }
                    //  var rs1 = Core.Helpers.WebHelper.HttpPostAsync(_appSettings.BaseUrls.Api + "brands/zalos/upsert/zalo-personal/" + id_zalo_business_id + "/" + id_zalo_channelid + "/" + profile_id + "/" + token + "/" + id_zalo_name + "/" + id_zalo_templateid, null).Result;

                    var access_token = Request.Cookies["access_token"] ?? "";
                    var url          = _appSettings.BaseUrls.Api + "brands/channels/list/" + id_zalo_business_id + "/?access_token=" + access_token;

                    var rs = Core.Helpers.WebHelper.HttpGetAsync <ChannelFeed>(url).Result;
                    if (rs == null || rs.Data == null)
                    {
                        rs = new ChannelFeed {
                            Data = new List <Channel>()
                        }
                    }
                    ;
                    var result = _viewRenderService.RenderToStringAsync("Channel/Settings", new ChannelSettings {
                        business_id = id_zalo_business_id, Channels = rs.Data, ZaloAuth = ""
                    }).Result;
                    response.msg  = "Add channel ok";
                    response.view = result;
                    response.ok   = true;

                    Response.Cookies.Delete("id_zalo_appid");
                    Response.Cookies.Delete("id_zalo_channelid");
                    Response.Cookies.Delete("id_zalo_business_id");
                    Response.Cookies.Delete("id_zalo_appsecret");
                    Response.Cookies.Delete("id_zalo_name");
                    Response.Cookies.Delete("id_zalo_templateid");
                    Response.Cookies.Delete("codeZalo");

                    return(response);
                }
                return(response);
            }
            catch (Exception ex)
            {
                response.msg = "Add channel error " + ex.Message;
                response.ok  = false;
                return(response);
            }
        }