Example #1
0
    public static string digest_HandleTokenResponse(string digest_in)
    {
        string digest = "";

#if (!DSV)
        digest = "RP[" + DSVHelper.code_to_hash(DSVHelper.return_code) + "((" + digest_in + "))]";
#endif
        return(digest);
    }
Example #2
0
    //**************************************digest computations*********************************************************************
    public static string digest_RequestAccessTokenByVerifier()
    {
        string digest = "";

#if (!DSV)
        digest = "RP[" + DSVHelper.code_to_hash(DSVHelper.request_code) + "(())" + "]";
        //since we don't have control of LiveID's server, we have to add its digest here
        digest = "MSFT[" + DSVHelper.code_to_hash(DSVHelper.live_code) + "((" + digest + "))" + "]";
#endif
        return(digest);
    }
Example #3
0
        public static void RequestAccessTokenByVerifier(string verifier, out OAuthToken token, out OAuthError error, out string digest)
        {
            digest = DSVHelper.digest_RequestAccessTokenByVerifier();

            string content = String.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code",
                                           HttpUtility.UrlEncode(clientId),
                                           HttpUtility.UrlEncode(callback),
                                           HttpUtility.UrlEncode(clientSecret),
                                           HttpUtility.UrlEncode(verifier));

            RequestAccessToken_wrapper(content, verifier, "authorization_code", out token, out error);
        }
Example #4
0
        public static void HandleTokenResponse(HttpContext context, OAuthToken token, OAuthError error, string digest, out string new_digest)
        {
            new_digest = DSVHelper.digest_HandleTokenResponse(digest);

            Dictionary <string, string> cookieValues = new Dictionary <string, string>();
            HttpCookie cookie    = context.Request.Cookies[wlCookie];
            HttpCookie newCookie = new HttpCookie(wlCookie);

            newCookie.Path = "/";

            //[I don't know why specifying domain will result in the cookie not set in the browser. My test domain is a.foo1234.com:54432.   ]
            //newCookie.Domain = context.Request.Headers["Host"];

            if (cookie != null && cookie.Values != null)
            {
                foreach (string key in cookie.Values.AllKeys)
                {
                    newCookie[key] = cookie[key];
                }
            }

            if (token != null)
            {
                userInfo = ReadUserInfoFromAuthToken(token);
                // The userInfo contains identifiable information about the user.
                // You may add some logic here.
                newCookie[OAuthConstants.AccessToken]         = HttpUtility.UrlEncode(token.AccessToken);
                newCookie[OAuthConstants.AuthenticationToken] = HttpUtility.UrlEncode(token.AuthenticationToken);
                newCookie[OAuthConstants.Scope]     = HttpUtility.UrlPathEncode(token.Scope);
                newCookie[OAuthConstants.ExpiresIn] = HttpUtility.UrlEncode(token.ExpiresIn);

                if (!string.IsNullOrEmpty(token.RefreshToken))
                {
                    SaveRefreshToken(token.RefreshToken);
                }
            }

            if (error != null)
            {
                newCookie[OAuthConstants.Error]            = HttpUtility.UrlEncode(error.Code);
                newCookie[OAuthConstants.ErrorDescription] = HttpUtility.UrlPathEncode(error.Description);
            }

            context.Response.Cookies.Add(newCookie);
        }
Example #5
0
        public static JsonWebToken userInfo = null;    //the final result of authentication

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            if (!string.IsNullOrEmpty(Request.QueryString[OAuthConstants.AccessToken]))
            {
                // There is a token available already. It should be the token flow. Ignore it.
                return;
            }


            string     verifier = Request.QueryString[OAuthConstants.Code];
            OAuthToken token;
            OAuthError error;
            string     digest, final_digest;

            if (!string.IsNullOrEmpty(verifier))
            {
                RequestAccessTokenByVerifier(verifier, out token, out error, out digest);
                HandleTokenResponse(context, token, error, digest, out final_digest);
#if (!DSV)
                bool result = DSVHelper.DSV_Check(final_digest);
#endif

                /* Note:
                 * The UserID cookie is only a ad-hoc way to communicate back to the browser in order to display (for the demo purpose).
                 * The server-side logic shoudn't rely on this cookie for authentication.
                 *
                 * The real user ID to be used is Session["UserID"]
                 */

                HttpCookie newCookie = new HttpCookie("UserID");
                newCookie.Path = "/";
                if (result == true && userInfo != null)
                {
                    newCookie.Value   = HttpUtility.UrlEncode(userInfo.Claims.UserId);
                    Session["UserID"] = userInfo.Claims.UserId;
                }
                else
                {
                    newCookie.Value   = "";
                    newCookie.Expires = DateTime.Now.AddDays(-1d);
                    Session["UserID"] = "";
                }
                context.Response.Cookies.Add(newCookie);

                return;
            }

            string refreshToken = ReadRefreshToken();
            if (!string.IsNullOrEmpty(refreshToken))
            {
                RequestAccessTokenByRefreshToken(refreshToken, out token, out error);
                HandleTokenResponse(context, token, error, "", out final_digest);
                return;
            }

            string errorCode = Request.QueryString[OAuthConstants.Error];
            string errorDesc = Request.QueryString[OAuthConstants.ErrorDescription];

            if (!string.IsNullOrEmpty(errorCode))
            {
                error = new OAuthError(errorCode, errorDesc);
                HandleTokenResponse(context, null, error, "", out final_digest);
            }
        }