public async Task<IHttpActionResult> LogInExternal(RegisterExternalBindingModel model) { if(!ModelState.IsValid) { return BadRequest( ModelState ); } if(User.Identity.IsAuthenticated == true) { if(User.Identity.Name == model.Email) { return Ok("Already Authenticated"); } else { return Unauthorized(); } } else { try { var user = await UserManager.FindAsync(new UserLoginInfo("microsoft", model.ExternalAccessToken)); if( user != null) { //Return Token (GUID of User) return Ok(user.Id); } else { user = new MyUser { UserName = model.Id, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; user.Logins.Add( new IdentityUserLogin { LoginProvider = "microsoft", ProviderKey = model.ExternalAccessToken } ); IdentityResult result = await UserManager.CreateAsync( user ); IHttpActionResult errorResult = GetErrorResult( result ); if ( errorResult != null ) { return errorResult; } //Return Token (GUID of User) return Ok( user.Id ); } } catch(Exception error) { return BadRequest(error.Message); } } }
public async Task SignIn(RegisterExternalBindingModel model) { //Check Model State if(!ModelState.IsValid) { //If Model State is not Valid, call error on Caller. Clients.Caller.error("Invalid Model: " + ModelState); } else { //Else, attempt logging the user try { //Find the User using the Log In Information var user = await UserManager.FindAsync( new UserLoginInfo( "microsoft", model.ExternalAccessToken ) ); //If User and Login exists, return the Token. if(user != null) { Clients.Caller.token(user.Id); } else { //Else, the User will be created. user = new MyUser { //UserName just needs to be unique. We really want an email address. UserName = model.Id, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Profile = new Profile() }; //The External Login is Added to the user user.Logins.Add(new IdentityUserLogin { LoginProvider = "microsoft", ProviderKey = model.ExternalAccessToken }); //Save the new User to the Database. IdentityResult result = await UserManager.CreateAsync( user ); //Check to see if User was successfully saved if(result.Succeeded) { //If User was saved, return the Token. Clients.Caller.token( user.Id ); } else { //Else, something went wrong with registering user. Abort operation and call error method on Caller. Clients.Caller.error("Registration Failed"); } } } //For Entity Validation Exception Errors, output the validation errors. catch ( DbEntityValidationException error ) { string output = "Exception Occurred: " + error.Message + ":"; //Return all error messages to error method on Caller. foreach ( var validationErrors in error.EntityValidationErrors ) { foreach ( var validationError in validationErrors.ValidationErrors ) { output += "Property: " + validationError.PropertyName + ", Error: " + validationError.ErrorMessage + " ------"; } } Clients.Caller.error( output ); } //Catch all other Exceptions to avoid silent failure catch ( Exception error ) { string output = "Exception Occurred: " + error.Message + " : "; while(error.InnerException != null) { output += "Inner Exception: " + error.InnerException.Message; error = error.InnerException; } //Return all error messages to error method on Caller. Clients.Caller.error( output ); } } }