1818using Microsoft . AspNetCore . WebUtilities ;
1919using Microsoft . Extensions . Logging ;
2020using AdefHelpDeskBase . Models ;
21+ using AdefHelpDeskBase . Models . DataContext ;
22+ using Microsoft . EntityFrameworkCore ;
2123
2224//#1 If there is an external login just log the person in
2325//#2 If there is not an existing external login, but there is an existing account (based on email not username) require the person to enter their existing username and password to associate the account and create an external login
@@ -34,20 +36,23 @@ public class ExternalLoginModel : PageModel
3436 private readonly IUserEmailStore < ApplicationUser > _emailStore ;
3537 private readonly IEmailSender _emailSender ;
3638 private readonly ILogger < ExternalLoginModel > _logger ;
39+ private IConfiguration _configuration { get ; set ; }
3740
3841 public ExternalLoginModel (
3942 SignInManager < ApplicationUser > signInManager ,
4043 UserManager < ApplicationUser > userManager ,
4144 IUserStore < ApplicationUser > userStore ,
4245 ILogger < ExternalLoginModel > logger ,
43- IEmailSender emailSender )
46+ IEmailSender emailSender ,
47+ IConfiguration configuration )
4448 {
4549 _signInManager = signInManager ;
4650 _userManager = userManager ;
4751 _userStore = userStore ;
4852 _emailStore = GetEmailStore ( ) ;
4953 _logger = logger ;
5054 _emailSender = emailSender ;
55+ _configuration = configuration ;
5156 }
5257
5358 [ BindProperty ]
@@ -153,22 +158,54 @@ public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null
153158 if ( ModelState . IsValid )
154159 {
155160 var user = CreateUser ( ) ;
161+
156162 await _userStore . SetUserNameAsync ( user , Input . Email , CancellationToken . None ) ;
157163 await _emailStore . SetEmailAsync ( user , Input . Email , CancellationToken . None ) ;
158164
159165 var result = await _userManager . CreateAsync ( user ) ;
166+
160167 if ( result . Succeeded )
161168 {
162169 result = await _userManager . AddLoginAsync ( user , info ) ;
170+
163171 if ( result . Succeeded )
164172 {
165- await _signInManager . SignInAsync ( user , isPersistent : false ) ;
166- return LocalRedirect ( returnUrl ) ;
173+ // Add user to ADefHelpDesk_Users table
174+ // Create Account ****************************
175+
176+ try
177+ {
178+ var optionsBuilder = new DbContextOptionsBuilder < ADefHelpDeskContext > ( ) ;
179+ optionsBuilder . UseSqlServer ( GetConnectionString ( ) ) ;
180+
181+ using ( var context = new ADefHelpDeskContext ( optionsBuilder . Options ) )
182+ {
183+ AdefHelpDeskUsers objAdefHelpDeskUsers = new AdefHelpDeskUsers ( ) ;
184+ objAdefHelpDeskUsers . Username = user . Email ;
185+ objAdefHelpDeskUsers . Email = user . Email ;
186+ objAdefHelpDeskUsers . FirstName = user . DisplayName ?? "" ;
187+ objAdefHelpDeskUsers . LastName = "" ;
188+ objAdefHelpDeskUsers . Password = "" ; // No longer store the password here
189+
190+ context . AdefHelpDeskUsers . Add ( objAdefHelpDeskUsers ) ;
191+ context . SaveChanges ( ) ;
192+ }
193+
194+ // *******************************************
195+ await _signInManager . SignInAsync ( user , isPersistent : false ) ;
196+ return LocalRedirect ( returnUrl ) ;
197+ }
198+ catch ( Exception ex )
199+ {
200+ // Return the error
201+ ModelState . AddModelError ( string . Empty , ex . GetBaseException ( ) . Message ) ;
202+ }
167203 }
168204 }
169205
170206 // Step #2: If there's an existing account, ask for password association
171207 var existingUser = await _userManager . FindByEmailAsync ( Input . Email ) ;
208+
172209 if ( existingUser != null )
173210 {
174211 if ( existingUser . PasswordHash != null )
@@ -193,6 +230,7 @@ public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null
193230 public async Task < IActionResult > OnPostAssociateLoginAsync ( string returnUrl = null )
194231 {
195232 returnUrl = returnUrl ?? Url . Content ( "~/" ) ;
233+
196234 var info = await _signInManager . GetExternalLoginInfoAsync ( ) ;
197235 if ( info == null )
198236 {
@@ -201,6 +239,7 @@ public async Task<IActionResult> OnPostAssociateLoginAsync(string returnUrl = nu
201239 }
202240
203241 var user = await _userManager . FindByEmailAsync ( Input . Email ) ;
242+
204243 if ( user != null && Input . Password != null )
205244 {
206245 var passwordCheck = await _userManager . CheckPasswordAsync ( user , Input . Password ) ;
@@ -233,7 +272,7 @@ public async Task<IActionResult> OnPostAssociateLoginAsync(string returnUrl = nu
233272 return Page ( ) ;
234273 }
235274
236-
275+ #region Utility
237276 private ApplicationUser CreateUser ( )
238277 {
239278 try
@@ -260,5 +299,23 @@ private IUserEmailStore<ApplicationUser> GetEmailStore()
260299 }
261300 return ( IUserEmailStore < ApplicationUser > ) _userStore ;
262301 }
302+
303+ private string GetConnectionString ( )
304+ {
305+ // Use this method to make sure we get the latest one
306+ string strConnectionString = "ERRROR:UNSET-CONECTION-STRING" ;
307+
308+ try
309+ {
310+ strConnectionString = _configuration [ "ConnectionStrings:DefaultConnection" ] ;
311+ }
312+ catch
313+ {
314+ // Do nothing
315+ }
316+
317+ return strConnectionString ;
318+ }
319+ #endregion
263320 }
264321}
0 commit comments