2017-08-25 17:55:26 +02:00
using System ;
using System.Configuration ;
2016-05-29 15:46:42 +01:00
using System.DirectoryServices.AccountManagement ;
using System.Threading.Tasks ;
using Umbraco.Core.Models.Identity ;
2020-01-07 13:50:38 +01:00
using Umbraco.Web.Models.Identity ;
2016-05-29 15:46:42 +01:00
2018-08-29 01:15:46 +10:00
namespace Umbraco.Web.Security
2016-05-29 15:46:42 +01:00
{
2019-11-25 21:20:00 +11:00
// TODO: This relies on an assembly that is not .NET Standard (at least not at the time of implementation) :(
2016-05-29 15:46:42 +01:00
public class ActiveDirectoryBackOfficeUserPasswordChecker : IBackOfficeUserPasswordChecker
{
2017-08-25 17:55:26 +02:00
public virtual string ActiveDirectoryDomain
{
get
{
2019-01-31 12:05:56 +00:00
// TODO: Verify this AppSetting key is used in .NET Framework & canot be changed to Umbraco.Core. prefix
2016-05-30 11:15:03 +01:00
return ConfigurationManager . AppSettings [ "ActiveDirectoryDomain" ];
}
}
2016-05-29 15:46:42 +01:00
public Task < BackOfficeUserPasswordCheckerResult > CheckPasswordAsync ( BackOfficeIdentityUser user , string password )
{
bool isValid ;
2016-05-30 11:15:03 +01:00
using ( var pc = new PrincipalContext ( ContextType . Domain , ActiveDirectoryDomain ))
2016-05-29 15:46:42 +01:00
{
2017-09-23 10:08:18 +02:00
isValid = pc . ValidateCredentials ( user . UserName , password );
2017-08-25 17:55:26 +02:00
}
if ( isValid && user . HasIdentity == false )
{
2019-01-27 01:17:32 -05:00
// TODO: the user will need to be created locally (i.e. auto-linked)
2017-08-25 17:55:26 +02:00
throw new NotImplementedException ( "The user " + user . UserName + " does not exist locally and currently the " + typeof ( ActiveDirectoryBackOfficeUserPasswordChecker ) + " doesn't support auto-linking, see http://issues.umbraco.org/issue/U4-10181" );
2016-05-29 15:46:42 +01:00
}
var result = isValid
? BackOfficeUserPasswordCheckerResult . ValidCredentials
: BackOfficeUserPasswordCheckerResult . InvalidCredentials ;
return Task . FromResult ( result );
}
}
}