@@ -313,7 +313,6 @@ public async Task<Guid> CreateUser(String givenName,
313313 String phoneNumber ,
314314 Dictionary < String , String > claims ,
315315 List < String > roles ,
316- Boolean ? requireRegistrationEmail ,
317316 CancellationToken cancellationToken ) {
318317 Guid userId = Guid . NewGuid ( ) ;
319318
@@ -328,11 +327,11 @@ public async Task<Guid> CreateUser(String givenName,
328327 PhoneNumber = phoneNumber
329328 } ;
330329
331- // Set the password
332- // TODO: generate password when not supplied (use GenerateRandomPassword)
330+ String passwordValue = String . IsNullOrEmpty ( password ) ? SecurityServiceManager . GenerateRandomPassword ( this . UserManager . Options . Password ) : password ;
333331
334- // Hash the new password
335- newIdentityUser . PasswordHash = this . PasswordHasher . HashPassword ( newIdentityUser , password ) ;
332+ // Hash the default password
333+ newIdentityUser . PasswordHash =
334+ this . PasswordHasher . HashPassword ( newIdentityUser , passwordValue ) ;
336335
337336 if ( String . IsNullOrEmpty ( newIdentityUser . PasswordHash ) ) {
338337 throw new IdentityResultException ( "Error generating password hash value, hash was null or empty" , IdentityResult . Failed ( ) ) ;
@@ -706,7 +705,7 @@ public async Task ProcessPasswordResetRequest(String username,
706705 resetToken = UrlEncoder . Default . Encode ( resetToken ) ;
707706 String baseAddress = ConfigurationReader . GetValue ( "ServiceOptions" , "PublicOrigin" ) ;
708707 String uri = $ "{ baseAddress } /Account/ForgotPassword/Confirm?userName={ user . UserName } &resetToken={ resetToken } &clientId={ clientId } ";
709-
708+
710709 TokenResponse token = await this . GetToken ( cancellationToken ) ;
711710 SendEmailRequest emailRequest = this . BuildPasswordResetEmailRequest ( user , uri ) ;
712711 try {
@@ -717,6 +716,24 @@ public async Task ProcessPasswordResetRequest(String username,
717716 }
718717 }
719718
719+ public async Task SendWelcomeEmail ( String userName ,
720+ CancellationToken cancellationToken ) {
721+ IdentityUser i = await this . UserManager . FindByNameAsync ( userName ) ;
722+ await this . UserManager . RemovePasswordAsync ( i ) ;
723+ String generatedPassword = SecurityServiceManager . GenerateRandomPassword ( this . UserManager . Options . Password ) ;
724+ await this . UserManager . AddPasswordAsync ( i , generatedPassword ) ;
725+
726+ // Send Email
727+ TokenResponse token = await this . GetToken ( cancellationToken ) ;
728+ SendEmailRequest emailRequest = this . BuildWelcomeEmail ( i . Email , generatedPassword ) ;
729+ try {
730+ await this . MessagingServiceClient . SendEmail ( token . AccessToken , emailRequest , cancellationToken ) ;
731+ }
732+ catch ( Exception ex ) {
733+ Logger . LogError ( ex ) ;
734+ }
735+ }
736+
720737 [ ExcludeFromCodeCoverage ]
721738 public async Task Signout ( ) {
722739 await this . SignInManager . SignOutAsync ( ) ;
@@ -791,6 +808,36 @@ private SendEmailRequest BuildPasswordResetEmailRequest(IdentityUser user,
791808 return request ;
792809 }
793810
811+ private SendEmailRequest BuildWelcomeEmail ( String emailAddress ,
812+ String password ) {
813+ StringBuilder mesasgeBuilder = new StringBuilder ( ) ;
814+ mesasgeBuilder . AppendLine ( "<html><body>" ) ;
815+ mesasgeBuilder . AppendLine ( "<p>Welcome to Transaction Processing System</p>" ) ;
816+ mesasgeBuilder . AppendLine ( "<p></p>" ) ;
817+ mesasgeBuilder . AppendLine ( "<p>Please find below your user details:</p>" ) ;
818+ mesasgeBuilder . AppendLine ( "<table>" ) ;
819+ mesasgeBuilder . AppendLine ( "<tr><td><strong>User Name</strong></td></tr>" ) ;
820+ mesasgeBuilder . AppendLine ( $ "<tr><td id=\" username\" >{ emailAddress } </td></tr>") ;
821+ mesasgeBuilder . AppendLine ( "<tr><td><strong>Password</strong></td></tr>" ) ;
822+ mesasgeBuilder . AppendLine ( $ "<tr><td id=\" password\" >{ password } </td></tr>") ;
823+ mesasgeBuilder . AppendLine ( "</table>" ) ;
824+ mesasgeBuilder . AppendLine ( "</body></html>" ) ;
825+
826+ SendEmailRequest request = new ( ) {
827+ Body = mesasgeBuilder . ToString ( ) ,
828+ ConnectionIdentifier = Guid . NewGuid ( ) ,
829+ FromAddress = "golfhandicapping@btinternet.com" ,
830+ IsHtml = true ,
831+ Subject = "Welcome to Transaction Processing" ,
832+ ToAddresses = new List < String > {
833+ emailAddress ,
834+ "stuart_ferguson1@outlook.com"
835+ }
836+ } ;
837+
838+ return request ;
839+ }
840+
794841 /// <summary>
795842 /// Converts the users claims.
796843 /// </summary>
@@ -816,6 +863,47 @@ private async Task<List<String>> ConvertUsersRoles(IdentityUser identityUser) {
816863 return roles . ToList ( ) ;
817864 }
818865
866+ private static String GenerateRandomPassword ( PasswordOptions opts = null ) {
867+ if ( opts == null )
868+ opts = new PasswordOptions {
869+ RequiredLength = 8 ,
870+ RequiredUniqueChars = 4 ,
871+ RequireDigit = true ,
872+ RequireLowercase = true ,
873+ RequireNonAlphanumeric = true ,
874+ RequireUppercase = true
875+ } ;
876+
877+ String [ ] randomChars = {
878+ "ABCDEFGHJKLMNOPQRSTUVWXYZ" , // uppercase
879+ "abcdefghijkmnopqrstuvwxyz" , // lowercase
880+ "0123456789" , // digits
881+ "!@$?_-" // non-alphanumeric
882+ } ;
883+
884+ Random rand = new Random ( Environment . TickCount ) ;
885+ List < Char > chars = new List < Char > ( ) ;
886+
887+ if ( opts . RequireUppercase )
888+ chars . Insert ( rand . Next ( 0 , chars . Count ) , randomChars [ 0 ] [ rand . Next ( 0 , randomChars [ 0 ] . Length ) ] ) ;
889+
890+ if ( opts . RequireLowercase )
891+ chars . Insert ( rand . Next ( 0 , chars . Count ) , randomChars [ 1 ] [ rand . Next ( 0 , randomChars [ 1 ] . Length ) ] ) ;
892+
893+ if ( opts . RequireDigit )
894+ chars . Insert ( rand . Next ( 0 , chars . Count ) , randomChars [ 2 ] [ rand . Next ( 0 , randomChars [ 2 ] . Length ) ] ) ;
895+
896+ if ( opts . RequireNonAlphanumeric )
897+ chars . Insert ( rand . Next ( 0 , chars . Count ) , randomChars [ 3 ] [ rand . Next ( 0 , randomChars [ 3 ] . Length ) ] ) ;
898+
899+ for ( Int32 i = chars . Count ; i < opts . RequiredLength || chars . Distinct ( ) . Count ( ) < opts . RequiredUniqueChars ; i ++ ) {
900+ String rcs = randomChars [ rand . Next ( 0 , randomChars . Length ) ] ;
901+ chars . Insert ( rand . Next ( 0 , chars . Count ) , rcs [ rand . Next ( 0 , rcs . Length ) ] ) ;
902+ }
903+
904+ return new String ( chars . ToArray ( ) ) ;
905+ }
906+
819907 private async Task < TokenResponse > GetToken ( CancellationToken cancellationToken ) {
820908 // Get a token to talk to the estate service
821909 String clientId = ConfigurationReader . GetValue ( "AppSettings" , "ClientId" ) ;
0 commit comments