コード例 #1
0
ファイル: TokenManagerTest.cs プロジェクト: nedele44/summit
        public void Add_Test()
        {
            //tokentest添加一个权限后,是否生效
            Assert.AreEqual(Token.Admin | Token.SuperAdmin, Admin.Add(Token.SuperAdmin));

            Assert.AreEqual(Token.Admin | Token.SuperAdmin | Token.Blogger, Admin.Add(Token.Blogger));

            Assert.AreEqual(Token.Admin | Token.SuperAdmin | Token.Blogger | Token.NewBie, Admin.Add(Token.NewBie));

            Assert.AreEqual(Token.Admin | Token.SuperAdmin | Token.Blogger | Token.NewBie | Token.Registered, Admin.Add(Token.Registered));
        }
コード例 #2
0
            // UNTESTED

            /*
             * // 4.3. Resource Owner Password Credentials Grant
             * public static async Task<RestResponse<OAuthToken>> TokenFromPassword(
             *  string tokenEndpoint,
             *  string userName,
             *  string password,
             *  string scope = "")
             * {
             *  tokenEndpoint.ThrowIfNullOrEmpty();
             *  userName.ThrowIfNullOrEmpty();
             *  password.ThrowIfNullOrEmpty();
             *
             *  RestRequest request = new RestRequest().
             *      Url(tokenEndpoint).
             *      Post().
             *      Param("grant_type", OAuth2.GRANT_TYPE_PASSWORD).
             *      Param("username", userName).
             *      Param("password", password);
             *
             *  AddNonEmptyParam(request, "scope", scope);
             *  AddBasicAuthIfGiven(request, userName, password);
             *
             *  return await request.Fetch<OAuthToken>();
             * }
             *
             * // 4.4. Client Credentials Grant
             * public async Task<RestResponse<OAuthToken>> TokenFromCredentials(
             *  string tokenEndpoint,
             *  string userName,
             *  string password,
             *  string scope = "")
             * {
             *  tokenEndpoint.ThrowIfNullOrEmpty();
             *  userName.ThrowIfNullOrEmpty();
             *  password.ThrowIfNullOrEmpty();
             *
             *  RestRequest request = new RestRequest().
             *      Url(tokenEndpoint).
             *      Post().
             *      Param("grant_type", OAuth2.GRANT_TYPE_PASSWORD);
             *  AddNonEmptyParam(request, "scope", scope);
             *
             *  AddBasicAuthIfGiven(request, userName, password);
             *
             *  return await request.Fetch<OAuthToken>();
             * }
             */
            #endregion

            #region Refresh access token

            /// <summary>
            /// RFC 6749 6. Refreshing an Access Token
            /// </summary>
            /// <param name="refreshToken"></param>
            /// <param name="clientId"></param>
            ///
            /// <param name="clientSecret"></param>
            /// <param name="scope"></param>
            /// <param name="tokenEndpoint"></param>
            /// <returns></returns>
            public static async Task <RestResponse <OAuthToken> > RefreshAccessToken(
                string tokenEndpoint,
                string refreshToken,
                string clientId     = "",
                string clientSecret = "",
                string scope        = "",
                string userName     = "",
                string password     = "")
            {
                tokenEndpoint.ThrowIfNullOrEmpty();
                refreshToken.ThrowIfNullOrEmpty("A refresh token is needed to get a new access token.");

                RestRequest request = new RestRequest().
                                      Url(tokenEndpoint).
                                      Post().
                                      Param("refresh_token", refreshToken).
                                      Param("grant_type", OAuth2.GRANT_TYPE_REFRESH_TOKEN);

                AddNonEmptyParam(request, "client_id", clientId);
                AddNonEmptyParam(request, "client_secret", clientSecret);
                AddNonEmptyParam(request, "scope", scope);
                AddBasicAuthIfGiven(request, userName, password);

                var result = await request.Fetch <OAuthToken>();

                if (result.HasData)
                {
                    TokenManager.Add(clientId, clientSecret, tokenEndpoint, result.Data);
                }
                return(result);
            }
コード例 #3
0
            // 4.1.3. Access Token Request
            public static async Task <RestResponse <T> > TokenFromAuthorizationCode <T>(
                string tokenEndpoint,
                string code,
                string clientId,
                string clientSecret           = "",
                string redirectUri            = "",
                string userName               = "",
                string password               = "",
                bool addToDefaultTokenManager = true)
                where T : OAuthToken
            {
                tokenEndpoint.ThrowIfNullOrEmpty();
                code.ThrowIfNullOrEmpty("An authorization code is needed to get the first refresh token.");
                clientId.ThrowIfNullOrEmpty("A client id code is needed to get the first refresh token.");

                RestRequest request = new RestRequest().
                                      Url(tokenEndpoint).
                                      Post().
                                      Param("code", code).
                                      Param("client_id", clientId).
                                      Param("grant_type", OAuth2.GRANT_TYPE_AUTH_CODE);

                AddNonEmptyParam(request, "redirect_uri", redirectUri);
                AddNonEmptyParam(request, "client_secret", clientSecret);
                AddBasicAuthIfGiven(request, userName, password);

                var result = await request.Fetch <T>();

                if (result.HasData && addToDefaultTokenManager)
                {
                    TokenManager.Add(clientId, clientSecret, tokenEndpoint, result.Data);
                }
                return(result);
            }
コード例 #4
0
        public ResponseResult <User> ValidAndGet(string account, string password)
        {
            var user = WcfHelper.Using <ISsoService, User>(c => { return(c.ValidAndGet(account, password)); });

            if (user == null)
            {
                return(new ResponseResult <User>()
                {
                    Code = 2,
                    Msg = "用户名或密码不正确!"
                });
            }

            TokenManager tokenMng = new TokenManager();
            var          token    = tokenMng.Add(user);

            return(new ResponseResult <User>()
            {
                Code = 1,
                Tag = token
            });
        }