Exemplo n.º 1
0
        public string GetLogin(string code)
        {
            if (!string.IsNullOrEmpty(code))
            {
                DefaultDingTalkClient client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
                OapiGettokenRequest   request = new OapiGettokenRequest();
                request.Appkey    = "dingjl4zohfnzjnpqudc";
                request.Appsecret = "38H3T-CatuPEwnZOXq5ZhDIwqDFGye4vrxc36yLVZx7pfAumOmZBN0WSgbZB-A0-";
                request.SetHttpMethod("GET");
                OapiGettokenResponse response = client.Execute(request);
                string token = response.AccessToken;
                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
                OapiUserGetuserinfoRequest request1 = new OapiUserGetuserinfoRequest();
                request1.Code = code;
                request1.SetHttpMethod("GET");
                OapiUserGetuserinfoResponse response1 = client.Execute(request1, token);
                String userId = response1?.Userid ?? null;

                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
                OapiUserGetRequest request2 = new OapiUserGetRequest();
                request2.Userid = userId;
                request2.SetHttpMethod("GET");
                OapiUserGetResponse response2 = client.Execute(request2, token);
                string str = JsonConvert.SerializeObject(new { response2.Jobnumber, response2.ManagerUserId, response2.Name, response2.Roles, response2.Userid });
                return(response2.Body);
            }
            else
            {
                return("0");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取用户userid
        /// {
        /// "userid": "****",
        /// "sys_level": 1,
        /// "errmsg": "ok",
        /// "is_sys": true,
        /// "deviceId": "***",
        /// "errcode": 0
        /// }
        /// </summary>
        public static String GetDingTalkUserid(string requestAuthCode, string accessToken)
        {
            DefaultDingTalkClient      client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            OapiUserGetuserinfoRequest req    = new OapiUserGetuserinfoRequest();

            req.Code = requestAuthCode;
            req.SetHttpMethod("GET");
            OapiUserGetuserinfoResponse response = client.Execute(req, accessToken);

            return(response.Userid);
        }
Exemplo n.º 3
0
        public static OapiUserGetuserinfoResponse GetUserId(string Code, string Token)
        {
            IDingTalkClient            client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();

            request.Code = Code;
            request.SetHttpMethod("GET");
            OapiUserGetuserinfoResponse response = client.Execute(request, Token);

            return(response);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 取得当前用户ID,返回订订模型
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        private OapiUserGetuserinfoResponse GetCurrentUserIdModel(string access_token, string code)
        {
            IDingTalkClient            client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();

            request.Code = code;
            request.SetHttpMethod("GET");
            OapiUserGetuserinfoResponse response = client.Execute(request, DdOper.AccessToken);

            return(response);
        }
Exemplo n.º 5
0
        public IActionResult login(string authCode)
        {
            IDingTalkClient            client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            OapiUserGetuserinfoRequest req    = new OapiUserGetuserinfoRequest();

            req.Code = authCode;
            req.SetHttpMethod("GET");
            var access_token = AccessTokenUtils.GetToken();
            OapiUserGetuserinfoResponse response = client.Execute(req, access_token);

            string userId   = response.Userid;
            string userName = getUserName(access_token, userId);

            return(SuccessResult(new { userId, userName }));
        }
Exemplo n.º 6
0
        public string GetUserId(string authCode)
        {
            var client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            var request = new OapiUserGetuserinfoRequest {
                Code = authCode
            };

            request.SetHttpMethod("GET");
            var response = client.Execute(request, AccessToken);

            if (response.Errcode != 0)
            {
                throw new Exception(response.Errmsg);
            }

            return(response.Userid);
        }
Exemplo n.º 7
0
 /// <summary>
 /// 通过code获取用户
 /// </summary>
 /// <param name="accessToken"></param>
 /// <param name="code"></param>
 /// <returns></returns>
 public static string GetUserByCode(string accessToken, string code)
 {
     try
     {
         IDingTalkClient            client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
         OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
         request.Code = code;
         request.SetHttpMethod("GET");
         OapiUserGetuserinfoResponse response = client.Execute(request, accessToken);
         log.Info("GetUserByCode: " + response.Body);
         return(response.Userid);
     }
     catch (Exception ex)
     {
         log.Info(ex);
         return("");
     }
 }
Exemplo n.º 8
0
        public string GetUserId(string authCode)
        {
            SetAccessToken();

            IDingTalkClient            client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();

            request.Code = authCode;
            request.SetHttpMethod("GET");
            OapiUserGetuserinfoResponse response = client.Execute(request, this.access_Token);

            if (response.IsError)
            {
                return(string.Empty);
            }
            else
            {
                return(response.Userid);
            }
        }
Exemplo n.º 9
0
        private string getDingUserid(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return(string.Empty);
            }

            IDingTalkClient            client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();

            request.Code = code;
            request.SetHttpMethod("GET");
            OapiUserGetuserinfoResponse response = client.Execute(request, getDingAccessToken());

            if (response.IsError)
            {
                logger.LogError(response.Errmsg);
                return(string.Empty);
            }
            return(response.Userid);
        }
Exemplo n.º 10
0
 public string GetUserInfo(string code)
 {
     try
     {
         var _url   = $"{m_host}/user/getuserinfo";
         var client = new DefaultDingTalkClient(_url);
         OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
         request.Code = code;
         request.SetHttpMethod("GET");
         OapiUserGetuserinfoResponse response = client.Execute(request, access_token);
         if (response.Errcode != 0)
         {
             // this.AppendDebugLog($"请求地址[{_url}],参数code=[{code}],返回{response.ToJson()}");
         }
         return(response.Userid);
     }
     catch (Exception ex)
     {
         //this.AppendErrorLog("请求服务器异常", ex);
     }
     return(null);
 }
Exemplo n.º 11
0
        /// <summary>
        /// 通过CODE换取用户身份UserId
        /// </summary>
        /// <param name="authCode"></param>
        /// <returns></returns>
        public static string getUseridByAuthCode(string accessToken, string authCode)
        {
            try
            {
                IDingTalkClient            client  = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
                OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
                request.Code = authCode;
                request.SetHttpMethod("GET");
                OapiUserGetuserinfoResponse response = client.Execute(request, accessToken);

                if (response != null && response.Errcode == 0)
                {
                    return(response.Userid);
                }
                else
                {
                    throw new Exception(response.Errmsg);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemplo n.º 12
0
        public override Task GrantClientCredentials(OAuthGrantClientCredentialsContext context)
        {
            YiFang_CustomerComplaintEntities dbContext = new YiFang_CustomerComplaintEntities();

            try
            {
                //var data = await context.Request.ReadFormAsync();
                var    formData = context.Request.ReadFormAsync();
                string Code     = formData.Result["Code"];
                string CS       = formData.Result["CS"];
                //ClaimsIdentity oAuthIdentity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
                ////用户名
                //oAuthIdentity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, "19423657671291041"));
                ////设置授权凭据
                //AuthenticationProperties properties = CreateProperties("19423657671291041");
                //AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                //context.Validated(ticket);
                //return base.GrantClientCredentials(context);
                //Code临时授权码为null执行微信登录,不为null执行钉钉登录
                if (!string.IsNullOrEmpty(Code))
                {
                    if (CS == "CS")
                    {
                        ClaimsIdentity oAuthIdentity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
                        //用户名
                        oAuthIdentity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, Code));
                        //设置授权凭据
                        AuthenticationProperties properties = CreateProperties(Code);
                        AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);
                        context.Validated(ticket);
                    }
                    else
                    {
                        DefaultDingTalkClient      defaultDingTalk = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
                        OapiUserGetuserinfoRequest req             = new OapiUserGetuserinfoRequest();
                        req.Code = Code;
                        req.SetHttpMethod("GET");
                        OapiUserGetuserinfoResponse execute = defaultDingTalk.Execute(req, AccessToken.GetAccessToken());
                        if (execute.Errcode != 0)
                        {
                            DingTalk.Core.Logs.LogHelper.WriteLog(execute.Body);
                            context.SetError("授权码出错啦或配置错误");
                            return(base.GrantClientCredentials(context));
                        }
                        string userid = execute.Userid;
                        var    Person = dbContext.OR_Person.FirstOrDefault(p => p.LoginName == userid);
                        if (Person == null)
                        {
                            context.SetError("该人员不在组织中");
                            return(base.GrantClientCredentials(context));
                        }
                        ClaimsIdentity oAuthIdentity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
                        //用户名
                        oAuthIdentity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, userid));
                        //设置授权凭据
                        AuthenticationProperties properties = CreateProperties(userid);
                        AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);
                        context.Validated(ticket);
                    }
                    //return base.GrantClientCredentials(context);
                    #region 钉钉登录
                    //if (Code=="123")
                    //{
                    //    ClaimsIdentity oAuthIdentity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
                    //                                                                         //010742350933650042
                    //    oAuthIdentity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, "010742350933650042"));
                    //    AuthenticationProperties properties = CreateProperties("010742350933650042");
                    //    AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                    //    context.Validated(ticket);
                    //    return base.GrantClientCredentials(context);
                    //}

                    //var AccessToken= Jusoft.YiFang.Db.ThirdSystem.AccessToken.GetAccessToken();
                    //if (string.IsNullOrEmpty(AccessToken))
                    //{
                    //    context.SetError("AccessToken", $"Code【{Code}】获取token失败");
                    //    return base.GrantClientCredentials(context);
                    //}
                    //var resUserId= Jusoft.YiFang.Db.ThirdSystem.AccessToken.GetUserId(Code, AccessToken);
                    //if (resUserId.Errcode!=0)
                    //{
                    //    context.SetError("resUserId", $"Code【{Code}】"+resUserId.Errmsg);
                    //    return base.GrantClientCredentials(context);
                    //}
                    //var Person = dbContext.OR_Person.FirstOrDefault(p=>p.LoginName == resUserId.Userid);
                    //if (Person != null)
                    //{
                    //    ClaimsIdentity oAuthIdentity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
                    //    oAuthIdentity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, Person.LoginName));
                    //    AuthenticationProperties properties = CreateProperties(Person.LoginName);
                    //    AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                    //    context.Validated(ticket);
                    //}
                    //else
                    //{
                    //    context.SetError("Person", $"Code【{Code}】未找到人员,请联系管理员");
                    //    return base.GrantClientCredentials(context);
                    //}
                    #endregion
                }
                else
                {
                    #region 微信登录
                    //string userid = formData.Result["username"];//用户名
                    //string password = formData.Result["password"];//密码
                    //string openid = formData.Result["openid"];//微信openid
                    //                                          //优先校验openid
                    //if (string.IsNullOrEmpty(openid))
                    //{
                    //    context.SetError("invalid_grant", "openid不合法");
                    //    return base.GrantClientCredentials(context);
                    //}
                    //if (!string.IsNullOrEmpty(userid))
                    //{
                    //    var person = dbContext.OR_Person.FirstOrDefault(k => k.LoginName == userid);
                    //    if (person == null)
                    //    {
                    //        context.SetError("1001", "门店账号信息不对,请重新输入");
                    //        return base.GrantClientCredentials(context);
                    //    }
                    //    else if (!string.IsNullOrEmpty(person.WeChatOpenId))
                    //    {
                    //        context.SetError("1001", $"门店账号信息已绑定用户,请联系管理员操作");
                    //        return base.GrantClientCredentials(context);
                    //    }
                    //    if (!dbContext.AC_SysUsers.Any(k => k.UserName == userid && k.PasswordHash == password))
                    //    {
                    //        context.SetError("1001", "门店密码信息不对,请重新输入");
                    //        return base.GrantClientCredentials(context);
                    //    }
                    //    person.WeChatOpenId = openid;
                    //    dbContext.SaveChanges();
                    //}
                    //else
                    //{
                    //    var person = dbContext.OR_Person.FirstOrDefault(k => k.WeChatOpenId == openid);
                    //    if (person == null)
                    //    {
                    //        context.SetError("1002", "用户还未绑定账号,请先绑定");
                    //        return base.GrantClientCredentials(context);
                    //    }
                    //    userid = person.LoginName;
                    //}

                    ////TODO: 校验该用户是否存在与我们自身的系统之中,若存在,则正常加入凭据信息
                    //var oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
                    //oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, userid));
                    //var ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties());
                    //context.Validated(ticket);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                context.SetError("invalid_grant", ex.ToString());
            }
            return(base.GrantClientCredentials(context));
        }