//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.AspNet {
using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Web;
using DotNetOpenAuth.Messaging;
///
/// Manage authenticating with an external OAuth or OpenID provider
///
public class OpenAuthSecurityManager {
private const string ProviderQueryStringName = "__provider__";
private readonly HttpContextBase _requestContext;
private readonly IOpenAuthDataProvider _dataProvider;
private readonly IAuthenticationClient _authenticationProvider;
///
/// Initializes a new instance of the class.
///
/// The request context.
public OpenAuthSecurityManager(HttpContextBase requestContext) :
this(requestContext, provider: null, dataProvider: null) {
}
///
/// Initializes a new instance of the class.
///
/// The request context.
/// The provider.
/// The data provider.
public OpenAuthSecurityManager(HttpContextBase requestContext, IAuthenticationClient provider, IOpenAuthDataProvider dataProvider) {
if (requestContext == null) {
throw new ArgumentNullException("requestContext");
}
_requestContext = requestContext;
_dataProvider = dataProvider;
_authenticationProvider = provider;
}
///
/// Requests the specified provider to start the authentication by directing users to an external website
///
/// The return url after user is authenticated.
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);
}
// 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];
}
///
/// Checks if user is successfully authenticated when user is redirected back to this user.
///
///
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;
}
///
/// Checks if the specified provider user id represents a valid account.
/// If it does, log user in.
///
/// The provider user id.
/// if set to true create persistent cookie.
///
/// true if the login is successful.
///
[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)) {
return false;
}
OpenAuthAuthenticationTicketHelper.SetAuthenticationTicket(_requestContext, userName, createPersistentCookie);
return true;
}
///
/// Gets a value indicating whether the current user is authenticated by an OAuth & OpenID provider.
///
public bool IsAuthenticatedWithOpenAuth {
get {
return _requestContext.Request.IsAuthenticated &&
OpenAuthAuthenticationTicketHelper.IsValidAuthenticationTicket(_requestContext);
}
}
}
}