Ejemplo n.º 1
0
        /// <summary>
        /// Parses access code response.
        /// </summary>
        /// <param name="requestUri">Request URI.</param>
        /// <returns>Access code response.</returns>
        public OAuthCodeResponse ParseAccessCodeResponse(System.Uri requestUri)
        {
            OAuthCodeResponse   ret   = new OAuthCodeResponse();
            NameValueCollection query = System.Web.HttpUtility.ParseQueryString(requestUri.Query ?? string.Empty);

            ret.Error      = System.Web.HttpUtility.UrlDecode(query["error"]);
            ret.AccessCode = System.Web.HttpUtility.UrlDecode(query["code"]);
            ret.State      = OAuthState.Parse(System.Web.HttpUtility.UrlDecode(query["state"]));

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles authorization request.
        /// </summary>
        /// <param name="requestUri">Request URI.</param>
        public void HandleRequest(System.Uri requestUri)
        {
            User              u         = null;
            OAuthToken        token     = null;
            PropertyInfo      prop      = null;
            OAuthToken        prevToken = null;
            OAuthCodeResponse code      = this.Channel.ParseAccessCodeResponse(requestUri);

            if (!string.IsNullOrEmpty(code.AccessCode) && code.State != null && code.State.UserId > 0)
            {
                token = this.Channel.GetAccessToken(requestUri, this.Name, code.AccessCode);

                if (!string.IsNullOrEmpty(token.AccessToken))
                {
                    using (var repo = Resolver.Resolve <IUserRepository>())
                    {
                        u    = repo.Select(code.State.UserId);
                        prop = GetClientAuthorizationProperty();

                        if (prop != null)
                        {
                            if (u.ApiAuthorization == null)
                            {
                                u.ApiAuthorization = new UserApiAuthorization();
                            }

                            if (string.IsNullOrEmpty(token.RefreshToken))
                            {
                                prevToken = prop.GetValue(u.ApiAuthorization) as OAuthToken;

                                if (prevToken != null && !string.IsNullOrEmpty(prevToken.RefreshToken))
                                {
                                    token.RefreshToken = prevToken.RefreshToken;
                                }
                            }

                            prop.SetValue(u.ApiAuthorization, token);

                            repo.Update(u);
                        }
                    }
                }
            }
        }