/// <summary> /// Returns the authorization URL. /// </summary> /// <param name="requestUri">Request URI.</param> /// <param name="scope">Scope.</param> /// <param name="applicationName">Application name.</param> /// <param name="userId">User Id.</param> /// <returns>Authorization URL.</returns> public string GetAuthorizationUrl(System.Uri requestUri, string scope, string applicationName, int userId) { OAuthState state = new OAuthState(applicationName, userId); return(string.Format("https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={0}&redirect_uri={1}&scope={2}&access_type=offline&state={3}", System.Web.HttpUtility.UrlEncode(ClientId), System.Web.HttpUtility.UrlEncode(GetReturnUrl(requestUri, applicationName)), System.Web.HttpUtility.UrlEncode(scope), System.Web.HttpUtility.UrlEncode(state.ToString()))); }
/// <summary> /// Parses OAuth state from the given input. /// </summary> /// <param name="input">Input.</param> /// <returns>OAuth state.</returns> public static OAuthState Parse(string input) { int userId = 0; string[] parts = null; OAuthState ret = new OAuthState(); if (!string.IsNullOrEmpty(input) && input.IndexOf(':') > 0) { parts = input.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (parts != null && parts.Length == 3 && int.TryParse(parts[0], out userId)) { ret.UserId = userId; ret.ApplicationName = parts[1]; if (string.Compare(Utils.Crypto.GetHash(userId.ToString() + _salt + parts[1]), parts[2], StringComparison.Ordinal) != 0) { ret.UserId = 0; ret.ApplicationName = string.Empty; } } } return(ret); }
/// <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); }