//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.AspNet.Clients {
using System.Web;
using System.Web.Security;
///
/// Stores OAuth tokens in the current request's cookie.
///
///
/// This class is different from the in that
/// it also stores the access token after the authentication has succeeded.
///
public class CookieOAuthTokenManager : AuthenticationOnlyCookieOAuthTokenManager {
///
/// Initializes a new instance of the class.
///
public CookieOAuthTokenManager() {
}
///
/// Initializes a new instance of the class.
///
/// The current request context.
public CookieOAuthTokenManager(HttpContextBase context)
: base(context) {
}
///
/// Gets the token secret from the specified token.
///
/// The token.
///
/// The token's secret
///
public override string GetTokenSecret(string token) {
string secret = base.GetTokenSecret(token);
if (secret != null) {
return secret;
}
// The base class checks for cookies in the Request object.
// Here we check in the Response object as well because we
// may have set it earlier in the request life cycle.
HttpCookie cookie = this.Context.Response.Cookies[TokenCookieKey];
if (cookie == null || string.IsNullOrEmpty(cookie.Values[token])) {
return null;
}
secret = DecodeAndUnprotectToken(token, cookie.Values[token]);
return secret;
}
///
/// Replaces the request token with access token.
///
/// The request token.
/// The access token.
/// The access token secret.
public override void ReplaceRequestTokenWithAccessToken(string requestToken, string accessToken, string accessTokenSecret) {
var cookie = new HttpCookie(TokenCookieKey) {
HttpOnly = true
};
if (FormsAuthentication.RequireSSL) {
cookie.Secure = true;
}
var encryptedToken = ProtectAndEncodeToken(accessToken, accessTokenSecret);
cookie.Values[accessToken] = encryptedToken;
this.Context.Response.Cookies.Set(cookie);
}
}
}