コード例 #1
0
        public GetTokenByPwdResponse GetAccessTokenByPwd()
        {
            if (!APIConfig.AuthorizeParameters.ContainsKey("appkey") ||
                !APIConfig.AuthorizeParameters.ContainsKey("appsecret") ||
                !APIConfig.AuthorizeParameters.ContainsKey("secerturl") ||
                !APIConfig.AuthorizeParameters.ContainsKey("userName") ||
                !APIConfig.AuthorizeParameters.ContainsKey("password") ||
                !APIConfig.AuthorizeParameters.ContainsKey("accNum")
                )
            {
                throw new Exception("鉴权参数不完整");
            }
            var    signClass      = new TokenManage();
            var    request        = new GetTokenByPwdRequest();
            string appkey         = APIConfig.AuthorizeParameters["appkey"];
            string appsecret      = APIConfig.AuthorizeParameters["appsecret"];
            string secetrurl      = APIConfig.AuthorizeParameters["secerturl"];
            string userName       = APIConfig.AuthorizeParameters["userName"];
            string password       = APIConfig.AuthorizeParameters["password"];
            string EncodePassword = signClass.GetMd5(password);
            string accNum         = APIConfig.AuthorizeParameters["accNum"];

            Dictionary <string, object> parm = new Dictionary <string, object>();

            parm.Add("appkey", appkey);
            parm.Add("orgid", "");
            parm.Add("appsecret", appsecret);

            JsonSerializer jsonSerializer = new JsonSerializer();
            string         datas          = jsonSerializer.Serialize(parm);

            try
            {
                string signvalue = signClass.CreateSignedToken(datas, secetrurl);
                string authStr   = @"{""appKey"":""" + appkey + @""",""authInfo"":""" + signvalue + @""",""orgId"":""""}";
                string encode    = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(authStr));
                Dictionary <string, string> parms = new Dictionary <string, string>();
                parms.Add("Authorization", encode);
                request.SetHeaderParameters(parms);

                Dictionary <string, object> postParms = new Dictionary <string, object>();
                var args = new PwdEntity()
                {
                    userName = userName, password = EncodePassword, accNum = accNum
                };
                var argsJson = jsonSerializer.Serialize(args);
                postParms.Add("_args", argsJson);
                request.SetPostParameters(postParms);

                var response = Excute(request);
                return(response);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
コード例 #2
0
        /// <summary>
        /// 设置指定用户的登录状态为离线
        /// </summary>
        /// <param name="id">用户账号</param>
        /// <returns>Result</returns>
        public Result <object> userSignOut(string id)
        {
            if (!verify())
            {
                return(result);
            }

            TokenManage.delete(tokenId);
            return(result);
        }
コード例 #3
0
        public HttpResponseMessage Post(InstancearchiveWrap instancearchiveWrap)
        {
            string          errMsg             = "";
            string          errMsgForChangeset = "";
            bool            isChangesetError   = false;
            Instancearchive model = instancearchiveWrap.Instancearchive;

            Model.Changeset changeset = new Model.Changeset();
            try
            {
                string token = this.ControllerContext.Request.Headers.GetValues("Token").First();
                auth = TokenManage.GetAuthorizer(token, out errMsg);
                //bool isChecked = TokenManage.CheckAuthorizer(token, PowerStatusCode.Manager, out auth, out errMsg);
                //车辆审核人及车辆管理员都可以归档车辆申请单
                if (auth.IsChecker || auth.IsManager)
                {
                    if (tryValidate(model, out errMsg))
                    {
                        if (this.tryPost(ref model, out errMsg))
                        {
                            if (!ChangesetManager.tryFetch(auth.Sync, ref changeset, out errMsgForChangeset))
                            {
                                isChangesetError = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            var    status = HttpStatusCode.OK;
            string json   = "";

            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new { errors = new { ServerSideError = errMsg } };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                model.Id = 1;
                status   = HttpStatusCode.OK;
                var msg = new { Instancearchive = model, Changeset = changeset, IsChangesetError = isChangesetError, ErrMsgForChangeset = errMsgForChangeset };
                json = JsonConvert.SerializeObject(msg);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
コード例 #4
0
        public HttpResponseMessage Post(OptimizerWrap optimizerWrap)
        {
            string    errMsg             = "";
            string    errMsgForChangeset = "";
            bool      isChangesetError   = false;
            Optimizer model = optimizerWrap.Optimizer;

            Model.Changeset changeset = new Model.Changeset();
            try
            {
                string token = this.ControllerContext.Request.Headers.GetValues("Token").First();
                auth = TokenManage.GetAuthorizer(token, out errMsg);
                //非扫码权限拥有者才可执行该功能
                bool isChecked = auth.IsAuthorized && !auth.IsScanner;
                if (isChecked)
                {
                    if (tryValidate(model, out errMsg))
                    {
                        if (this.tryPost(ref model, out errMsg))
                        {
                            if (!ChangesetManager.tryFetch(auth.Sync, ref changeset, out errMsgForChangeset))
                            {
                                isChangesetError = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            var    status = HttpStatusCode.OK;
            string json   = "";

            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new { errors = new { ServerSideError = errMsg } };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                model.Id = 1;
                status   = HttpStatusCode.OK;
                var msg = new { Optimizer = model, Changeset = changeset, IsChangesetError = isChangesetError, ErrMsgForChangeset = errMsgForChangeset };
                json = JsonConvert.SerializeObject(msg);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
コード例 #5
0
        public HttpResponseMessage Put(int id, PreferenceWrap preferenceWrap)
        {
            string     errMsg             = "";
            string     errMsgForChangeset = "";
            bool       isChangesetError   = false;
            Preference model = preferenceWrap.Preference;

            model.Id = id;
            Model.Changeset changeset = new Model.Changeset();
            try
            {
                string token     = this.ControllerContext.Request.Headers.GetValues("Token").First();
                bool   isChecked = TokenManage.CheckAuthorizer(token, PowerStatusCode.Systemer, out auth, out errMsg);
                if (isChecked)
                {
                    if (tryValidate(model, out errMsg))
                    {
                        if (this.tryPut(ref model, out errMsg))
                        {
                            if (!ChangesetManager.tryFetch(auth.Sync, ref changeset, out errMsgForChangeset))
                            {
                                isChangesetError = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            var    status = HttpStatusCode.OK;
            string json   = "";

            //errMsg = "网络。。。 ";
            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new { errors = new { ServerSideError = errMsg } };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                status = HttpStatusCode.OK;
                var msg = new { Preference = model, Changeset = changeset, IsChangesetError = isChangesetError, ErrMsgForChangeset = errMsgForChangeset };
                json = JsonConvert.SerializeObject(msg);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
コード例 #6
0
ファイル: Auth.cs プロジェクト: zwjhuhu/Insight_Base
        /// <summary>
        /// 移除指定账户的AccessToken
        /// </summary>
        /// <returns>Result</returns>
        public Result <object> removeToken()
        {
            var verify = new Verify();

            manage  = verify.manage;
            tokenId = verify.tokenId;
            if (manage == null)
            {
                return(result.invalidToken());
            }

            TokenManage.delete(tokenId);
            Core.setUserCache(manage);

            return(result.success());
        }
コード例 #7
0
        public string GetAuthorization(string tokenStr)
        {
            Dictionary <string, object> parm = new Dictionary <string, object>();
            string appkey    = APIConfig.AuthorizeParameters["appkey"];
            string orgid     = APIConfig.AuthorizeParameters["orgid"];
            string appsecret = APIConfig.AuthorizeParameters["appsecret"];
            string secetrurl = APIConfig.AuthorizeParameters["secerturl"];

            if (string.IsNullOrEmpty(secetrurl) || !File.Exists(secetrurl))
            {
                throw new Exception("请指定私钥路径!");
            }
            if (!APIConfig.AuthorizeParameters["userName"].IsNullOrEmpty())
            {
                orgid = "";
            }
            parm.Add("appkey", appkey);
            parm.Add("orgid", orgid);
            parm.Add("appsecret", appsecret);
            //业务请求的Authorization
            var customParas = new Dictionary <string, object>
            {
                { "access_token", tokenStr }
            };
            JsonSerializer jsonSerializer = new JsonSerializer();
            string         bizdatas       = jsonSerializer.Serialize(parm);
            var            signClass      = new TokenManage();

            try
            {
                string bizAuthorization = signClass.CreateSignedToken(bizdatas, secetrurl, customParas);
                string authStr1         = @"{""appKey"":""" + appkey + @""",""authInfo"":""" + bizAuthorization + @""",""orgId"":" + (!orgid.IsNullOrEmpty() ? orgid : @"""""") + @"}";
                string encode1          = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(authStr1));
                return(encode1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
コード例 #8
0
        public AccessTokenResponse GetAccessToken()
        {
            if (!APIConfig.AuthorizeParameters.ContainsKey("appkey") ||
                !APIConfig.AuthorizeParameters.ContainsKey("orgid") ||
                !APIConfig.AuthorizeParameters.ContainsKey("appsecret") ||
                !APIConfig.AuthorizeParameters.ContainsKey("secerturl"))
            {
                throw new Exception("鉴权参数不完整");
            }
            var request = new AccessTokenRequest();
            Dictionary <string, object> parm = new Dictionary <string, object>();
            string appkey    = APIConfig.AuthorizeParameters["appkey"];
            string orgid     = APIConfig.AuthorizeParameters["orgid"];
            string appsecret = APIConfig.AuthorizeParameters["appsecret"];
            string secetrurl = APIConfig.AuthorizeParameters["secerturl"];

            parm.Add("appkey", appkey);
            parm.Add("orgid", orgid);
            parm.Add("appsecret", appsecret);

            JsonSerializer jsonSerializer = new JsonSerializer();
            string         datas          = jsonSerializer.Serialize(parm);

            try
            {
                var    signClass = new TokenManage();
                string signvalue = signClass.CreateSignedToken(datas, secetrurl);
                string authStr   = @"{""appKey"":""" + appkey + @""",""authInfo"":""" + signvalue + @""",""orgId"":" + orgid + @"}";
                string encode    = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(authStr));
                Dictionary <string, string> parms = new Dictionary <string, string>();
                parms.Add("Authorization", encode);
                request.SetHeaderParameters(parms);
                var response = Excute(request);
                return(response);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
コード例 #9
0
        public HttpResponseMessage Delete(int id)
        {
            string errMsg = "";

            try
            {
                string token     = this.ControllerContext.Request.Headers.GetValues("Token").First();
                bool   isChecked = TokenManage.CheckAuthorizer(token, PowerStatusCode.Scanner, out auth, out errMsg);
                if (isChecked)
                {
                    if (id > 0)
                    {
                        this.tryDelete(id.ToString(), out errMsg);
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            var    status = HttpStatusCode.OK;
            string json   = "";

            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new { errors = new { ServerSideError = errMsg } };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                status = HttpStatusCode.OK;
                var msg = new { };
                json = JsonConvert.SerializeObject(msg);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
コード例 #10
0
        public HttpResponseMessage Get()
        {
            string errMsg = "";

            Model.Startup startup = new Model.Startup();
            try
            {
                string token = this.ControllerContext.Request.Headers.GetValues("Token").First();
                if (TokenManage.CheckAuthorizer(token, PowerStatusCode.None, out errMsg))
                {
                    StartupManager.tryFetch(ref startup, out errMsg);
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            var    status = HttpStatusCode.OK;
            string json   = "";

            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new { errors = new { ServerSideError = errMsg } };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                status = HttpStatusCode.OK;
                var msg = new { Startup = startup };
                json = JsonConvert.SerializeObject(msg);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
コード例 #11
0
        public void Login(HttpContext context)
        {
            //响应者
            HttpResponse Response = context.Response;
            //请求者
            HttpRequest Request = context.Request;
            //往返路径
            string returnUrl = Request["returnUrl"];

            //如果没有回调链接,该SSO不起作用
            if (string.IsNullOrEmpty(returnUrl))
            {
                return;
            }
            else
            {
                //判断returnUrl是否为信任的Domain
            }
            if (Request["UserName"] != "" && Request["password"] != "")
            {
                var pp = context.User.Identity.Name;

                string userName = Request["UserName"];
                string passWord = Request["password"];

                bool result = SmartAuthenticate.AuthenticateUser(userName, passWord, true);

                //SSOToken token = null;
                ////判断当前是否登录()
                //if (SmartAuthenticate.LoginUser != null)//未登录(生成token【携带用户信息】,并加入到集合里去)
                //{
                //    SmartAuthenticate.LoginUser.UserName = userName;
                //    token = TokenManage.SetToken(context.Session.SessionID);
                //}


                SSOToken token = null;
                //判断当前是否登录()
                if (!string.IsNullOrEmpty(userName))//未登录(生成token【携带用户信息】,并加入到集合里去)
                {
                    //SmartAuthenticate.LoginUser.UserName = userName;
                    token = TokenManage.SetToken(context.Session.SessionID, userName, passWord);
                }

                //拼接返回的url,参数中带Tip
                string spliter = returnUrl.Contains('?') ? "&" : "?";
                if (token != null)
                {
                    //将TIP返回
                    returnUrl = returnUrl + spliter + "token=" + token.ID + "&IsSuccessed=" + "true";
                }
                else
                {
                    //将TIP返回
                    returnUrl = returnUrl + spliter + "&IsSuccessed=" + "false";
                }

                string callback = context.Request["jsoncallback"];
                Response.Write(callback + "({\"result\":\"" + token.ID + "\"})");

                //跳转到访问该SSO的初始页面
                //Response.Redirect(returnUrl);
            }
        }
コード例 #12
0
        public HttpResponseMessage Post(SignatureWrap signatureWrap)
        {
            string    errMsg             = "";
            string    errMsgForChangeset = "";
            bool      isChangesetError   = false;
            Signature model     = signatureWrap.Signature;
            Changeset changeset = new Changeset();

            try
            {
                string token     = base.ControllerContext.Request.Headers.GetValues("Token").First <string>();
                bool   isChecked = TokenManage.CheckAuthorizer(token, PowerStatusCode.Scanner, out this.auth, out errMsg);
                if (isChecked)
                {
                    if (this.tryValidate(model, out errMsg))
                    {
                        if (this.tryPost(ref model, out errMsg))
                        {
                            if (!ChangesetManager.tryFetch(this.auth.Sync, ref changeset, out errMsgForChangeset))
                            {
                                isChangesetError = true;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                errMsg = ex.Message;
            }
            HttpStatusCode status;
            string         json;

            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new
                {
                    errors = new
                    {
                        ServerSideError = errMsg
                    }
                };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                status = HttpStatusCode.OK;
                var msg2 = new
                {
                    Signature          = model,
                    Changeset          = changeset,
                    IsChangesetError   = isChangesetError,
                    ErrMsgForChangeset = errMsgForChangeset
                };
                json = JsonConvert.SerializeObject(msg2);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
コード例 #13
0
        public HttpResponseMessage Post(AccountpwdWrap accountpwdWrap)
        {
            string     errMsg             = "";
            string     errMsgForChangeset = "";
            bool       isChangesetError   = false;
            Accountpwd accountpwd         = accountpwdWrap.Accountpwd;

            Model.Changeset changeset = new Model.Changeset();
            try
            {
                string token     = this.ControllerContext.Request.Headers.GetValues("Token").First();
                bool   isChecked = false;
                if (TokenManage.CheckAuthorizer(token, PowerStatusCode.None, out auth, out errMsg))
                {
                    if (auth.UserId == accountpwd.User)
                    {
                        isChecked = true;
                    }
                    else
                    {
                        errMsg = "密码不能被本人以外其他用户修改";
                    }
                }
                if (isChecked)
                {
                    if (tryValidate(accountpwd, out errMsg))
                    {
                        if (this.tryPost(ref accountpwd, out errMsg))
                        {
                            if (!ChangesetManager.tryFetch(auth.Sync, ref changeset, out errMsgForChangeset))
                            {
                                isChangesetError = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }
            var    status = HttpStatusCode.OK;
            string json   = "";

            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new { errors = new { ServerSideError = errMsg } };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                status = HttpStatusCode.OK;
                var msg = new { Accountpwd = accountpwd, Changeset = changeset, IsChangesetError = isChangesetError, ErrMsgForChangeset = errMsgForChangeset };
                json = JsonConvert.SerializeObject(msg);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }