コード例 #1
0
ファイル: HttpClientUnitTest.cs プロジェクト: nuscien/trivial
        public void TestHttpClientExtensions()
        {
            using var content = HttpClientExtensions.CreateJsonContent((byte) 0);
            Assert.IsNotNull(content);
            var mime = content.Headers.ContentType.ToString().YieldSplit(new List <string> {
                " "
            }).FirstOrDefault().YieldSplit(";", StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();

            Assert.AreEqual(Web.WebFormat.JsonMIME, mime);

            var oauth = new OAuthClient("someone", "secret", null);

            oauth.Scope.Add("test");
            oauth.Scope.Add("another");
            Assert.AreEqual("test another", oauth.ScopeString);
            oauth.ScopeString = "query";
            var oneSec = TimeSpan.FromSeconds(1);

            oauth.Timeout = oneSec;
            Assert.AreEqual(1, oauth.Scope.Count);
            var client = oauth.Create <JsonModel>();

            Assert.AreEqual(oneSec, client.Timeout);
        }
コード例 #2
0
 /// <summary>获取OAuth客户端</summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public virtual OAuthClient GetClient(String name) => OAuthClient.Create(name);
コード例 #3
0
        public ActionResult Login()
        {
            var returnUrl = GetRequest("r");

            // 如果已登录,直接跳转
            if (ManageProvider.User != null)
            {
                if (Url.IsLocalUrl(returnUrl))
                {
                    return(Redirect(returnUrl));
                }
                else
                {
                    return(RedirectToAction("Index", "Index", new { page = returnUrl }));
                }
            }

            // 是否已完成第三方登录
            var logId = Session["Cube_OAuthId"].ToLong();

            // 如果禁用本地登录,且只有一个第三方登录,直接跳转,构成单点登录
            var ms = OAuthConfig.GetValids();

            if (ms != null && !Setting.Current.AllowLogin)
            {
                if (ms.Count == 0)
                {
                    throw new Exception("禁用了本地密码登录,且没有配置第三方登录");
                }
                if (logId > 0)
                {
                    throw new Exception("已完成第三方登录,但无法绑定本地用户且没有开启自动注册,建议开启OAuth应用的自动注册");
                }

                // 只有一个,跳转
                if (ms.Count == 1)
                {
                    var url = $"~/Sso/Login?name={ms[0].Name}";
                    if (!returnUrl.IsNullOrEmpty())
                    {
                        url += "&r=" + HttpUtility.UrlEncode(returnUrl);
                    }

                    return(Redirect(url));
                }
            }

            // 部分提供支持应用内免登录,直接跳转
            if (ms != null && ms.Count > 0 && logId == 0 && GetRequest("autologin") != "0")
            {
#if __CORE__
                var agent = Request.Headers["User-Agent"] + "";
#else
                var agent = Request.UserAgent;
#endif
                if (!agent.IsNullOrEmpty())
                {
                    foreach (var item in ms)
                    {
                        var client = OAuthClient.Create(item.Name);
                        if (client != null && client.Support(agent))
                        {
                            var url = $"~/Sso/Login?name={item.Name}";
                            if (!returnUrl.IsNullOrEmpty())
                            {
                                url += "&r=" + HttpUtility.UrlEncode(returnUrl);
                            }

                            return(Redirect(url));
                        }
                    }
                }
            }

            //ViewBag.IsShowTip = XCode.Membership.User.Meta.Count == 1;
            //ViewBag.ReturnUrl = returnUrl;

            var model = GetViewModel(returnUrl);
            model.OAuthItems = ms.Where(e => e.Visible).ToList();

            return(View(model));
        }
コード例 #4
0
        public ActionResult Login()
        {
            var returnUrl = GetRequest("r");

            // 如果已登录,直接跳转
            if (ManageProvider.User != null)
            {
                if (Url.IsLocalUrl(returnUrl))
                {
                    return(Redirect(returnUrl));
                }
                else
                {
                    return(RedirectToAction("Index", "Index", new { page = returnUrl }));
                }
            }

            // 如果禁用本地登录,且只有一个第三方登录,直接跳转,构成单点登录
            var ms = OAuthConfig.GetValids();

            if (ms != null && !Setting.Current.AllowLogin)
            {
                if (ms.Count == 0)
                {
                    throw new Exception("禁用了本地密码登录,且没有配置第三方登录");
                }

                // 只有一个,跳转
                if (ms.Count == 1)
                {
                    var url = $"~/Sso/Login?name={ms[0].Name}";
                    if (!returnUrl.IsNullOrEmpty())
                    {
                        url += "&r=" + HttpUtility.UrlEncode(returnUrl);
                    }

                    return(Redirect(url));
                }
            }

            // 支持钉钉,且在钉钉内打开,直接跳转
            if (ms != null)
            {
#if __CORE__
                var agent = Request.Headers["User-Agent"] + "";
#else
                var agent = Request.UserAgent;
#endif
                if (!agent.IsNullOrEmpty())
                {
                    foreach (var item in ms)
                    {
                        var client = OAuthClient.Create(item.Name);
                        if (client != null && client.Support(agent))
                        {
                            var url = $"~/Sso/Login?name={item.Name}";
                            if (!returnUrl.IsNullOrEmpty())
                            {
                                url += "&r=" + HttpUtility.UrlEncode(returnUrl);
                            }

                            return(Redirect(url));
                        }
                    }
                }
            }

            ViewBag.IsShowTip = XCode.Membership.User.Meta.Count == 1;
            ViewBag.ReturnUrl = returnUrl;

            return(View());
        }