diff options
Diffstat (limited to 'src/DotNetOpenAuth.AspNet/OpenAuthSecurityManager.cs')
-rw-r--r-- | src/DotNetOpenAuth.AspNet/OpenAuthSecurityManager.cs | 174 |
1 files changed, 115 insertions, 59 deletions
diff --git a/src/DotNetOpenAuth.AspNet/OpenAuthSecurityManager.cs b/src/DotNetOpenAuth.AspNet/OpenAuthSecurityManager.cs index fefa3f5..6851c6d 100644 --- a/src/DotNetOpenAuth.AspNet/OpenAuthSecurityManager.cs +++ b/src/DotNetOpenAuth.AspNet/OpenAuthSecurityManager.cs @@ -7,7 +7,6 @@ namespace DotNetOpenAuth.AspNet { using System; using System.Diagnostics.CodeAnalysis; - using System.Diagnostics.Contracts; using System.Web; using DotNetOpenAuth.Messaging; @@ -15,104 +14,161 @@ namespace DotNetOpenAuth.AspNet { /// Manage authenticating with an external OAuth or OpenID provider /// </summary> public class OpenAuthSecurityManager { + #region Constants and Fields + + /// <summary> + /// The provider query string name. + /// </summary> private const string ProviderQueryStringName = "__provider__"; - private readonly HttpContextBase _requestContext; - private readonly IOpenAuthDataProvider _dataProvider; + /// <summary> + /// The _authentication provider. + /// </summary> private readonly IAuthenticationClient _authenticationProvider; /// <summary> + /// The _data provider. + /// </summary> + private readonly IOpenAuthDataProvider _dataProvider; + + /// <summary> + /// The _request context. + /// </summary> + private readonly HttpContextBase _requestContext; + + #endregion + + #region Constructors and Destructors + + /// <summary> /// Initializes a new instance of the <see cref="OpenAuthSecurityManager"/> class. /// </summary> - /// <param name="requestContext">The request context.</param> - public OpenAuthSecurityManager(HttpContextBase requestContext) : - this(requestContext, provider: null, dataProvider: null) { - } + /// <param name="requestContext"> + /// The request context. + /// </param> + public OpenAuthSecurityManager(HttpContextBase requestContext) + : this(requestContext, provider: null, dataProvider: null) {} /// <summary> /// Initializes a new instance of the <see cref="OpenAuthSecurityManager"/> class. /// </summary> - /// <param name="requestContext">The request context.</param> - /// <param name="provider">The provider.</param> - /// <param name="dataProvider">The data provider.</param> - public OpenAuthSecurityManager(HttpContextBase requestContext, IAuthenticationClient provider, IOpenAuthDataProvider dataProvider) { + /// <param name="requestContext"> + /// The request context. + /// </param> + /// <param name="provider"> + /// The provider. + /// </param> + /// <param name="dataProvider"> + /// The data provider. + /// </param> + public OpenAuthSecurityManager( + HttpContextBase requestContext, IAuthenticationClient provider, IOpenAuthDataProvider dataProvider) { if (requestContext == null) { throw new ArgumentNullException("requestContext"); } - _requestContext = requestContext; - _dataProvider = dataProvider; - _authenticationProvider = provider; + this._requestContext = requestContext; + this._dataProvider = dataProvider; + this._authenticationProvider = provider; } + #endregion + + #region Public Properties + /// <summary> - /// Requests the specified provider to start the authentication by directing users to an external website + /// Gets a value indicating whether IsAuthenticatedWithOpenAuth. /// </summary> - /// <param name="returnUrl">The return url after user is authenticated.</param> - public void RequestAuthentication(string returnUrl) { - // convert returnUrl to an absolute path - Uri uri; - if (!String.IsNullOrEmpty(returnUrl)) { - uri = UriHelper.ConvertToAbsoluteUri(returnUrl, _requestContext); - } else { - uri = HttpRequestInfo.GetPublicFacingUrl(_requestContext.Request, _requestContext.Request.ServerVariables); + public bool IsAuthenticatedWithOpenAuth { + get { + return this._requestContext.Request.IsAuthenticated + && OpenAuthAuthenticationTicketHelper.IsValidAuthenticationTicket(this._requestContext); } - // attach the provider parameter so that we know which provider initiated - // the login when user is redirected back to this page - uri = uri.AttachQueryStringParameter(ProviderQueryStringName, _authenticationProvider.ProviderName); - _authenticationProvider.RequestAuthentication(_requestContext, uri); } - public static string GetProviderName(HttpContextBase context) { - return context.Request.QueryString[ProviderQueryStringName]; - } + #endregion + + #region Public Methods and Operators /// <summary> - /// Checks if user is successfully authenticated when user is redirected back to this user. + /// The get provider name. /// </summary> - /// <returns></returns> - public AuthenticationResult VerifyAuthentication() { - AuthenticationResult result = _authenticationProvider.VerifyAuthentication(_requestContext); - if (!result.IsSuccessful) { - // if the result is a Failed result, creates a new Failed response which has providerName info. - result = new AuthenticationResult(isSuccessful: false, - provider: _authenticationProvider.ProviderName, - providerUserId: null, - userName: null, - extraData: null); - } - - return result; + /// <param name="context"> + /// The context. + /// </param> + /// <returns> + /// The get provider name. + /// </returns> + public static string GetProviderName(HttpContextBase context) { + return context.Request.QueryString[ProviderQueryStringName]; } /// <summary> - /// Checks if the specified provider user id represents a valid account. - /// If it does, log user in. + /// Checks if the specified provider user id represents a valid account. If it does, log user in. /// </summary> - /// <param name="providerUserId">The provider user id.</param> - /// <param name="createPersistentCookie">if set to <c>true</c> create persistent cookie.</param> + /// <param name="providerUserId"> + /// The provider user id. + /// </param> + /// <param name="createPersistentCookie"> + /// if set to <c>true</c> create persistent cookie. + /// </param> /// <returns> - /// <c>true</c> if the login is successful. + /// <c>true</c> if the login is successful. /// </returns> - [SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Login", Justification = "Login is used more consistently in ASP.Net")] + [SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Login", + Justification = "Login is used more consistently in ASP.Net")] public bool Login(string providerUserId, bool createPersistentCookie) { - string userName = _dataProvider.GetUserNameFromOpenAuth(_authenticationProvider.ProviderName, providerUserId); - if (String.IsNullOrEmpty(userName)) { + string userName = this._dataProvider.GetUserNameFromOpenAuth( + this._authenticationProvider.ProviderName, providerUserId); + if (string.IsNullOrEmpty(userName)) { return false; } - OpenAuthAuthenticationTicketHelper.SetAuthenticationTicket(_requestContext, userName, createPersistentCookie); + OpenAuthAuthenticationTicketHelper.SetAuthenticationTicket(this._requestContext, userName, createPersistentCookie); return true; } /// <summary> - /// Gets a value indicating whether the current user is authenticated by an OAuth & OpenID provider. + /// Requests the specified provider to start the authentication by directing users to an external website /// </summary> - public bool IsAuthenticatedWithOpenAuth { - get { - return _requestContext.Request.IsAuthenticated && - OpenAuthAuthenticationTicketHelper.IsValidAuthenticationTicket(_requestContext); + /// <param name="returnUrl"> + /// The return url after user is authenticated. + /// </param> + public void RequestAuthentication(string returnUrl) { + // convert returnUrl to an absolute path + Uri uri; + if (!string.IsNullOrEmpty(returnUrl)) { + uri = UriHelper.ConvertToAbsoluteUri(returnUrl, this._requestContext); + } else { + uri = HttpRequestInfo.GetPublicFacingUrl(this._requestContext.Request, this._requestContext.Request.ServerVariables); } + + // attach the provider parameter so that we know which provider initiated + // the login when user is redirected back to this page + uri = uri.AttachQueryStringParameter(ProviderQueryStringName, this._authenticationProvider.ProviderName); + this._authenticationProvider.RequestAuthentication(this._requestContext, uri); } + + /// <summary> + /// Checks if user is successfully authenticated when user is redirected back to this user. + /// </summary> + /// <returns> + /// </returns> + public AuthenticationResult VerifyAuthentication() { + AuthenticationResult result = this._authenticationProvider.VerifyAuthentication(this._requestContext); + if (!result.IsSuccessful) { + // if the result is a Failed result, creates a new Failed response which has providerName info. + result = new AuthenticationResult( + isSuccessful: false, + provider: this._authenticationProvider.ProviderName, + providerUserId: null, + userName: null, + extraData: null); + } + + return result; + } + + #endregion } -}
\ No newline at end of file +} |