public async Task<MyDriving.DataObjects.UserProfile> Get() { //return the current authenticated user profile ClaimsPrincipal user = User as ClaimsPrincipal; bool? isAuthenticated = user?.Identity?.IsAuthenticated; if (!isAuthenticated.GetValueOrDefault()) { return null; } var userId = string.Empty; // Get the credentials for the logged-in user. var fbCredentials = await user.GetAppServiceIdentityAsync<FacebookCredentials>(Request); var msCredentials = await user.GetAppServiceIdentityAsync<MicrosoftAccountCredentials>(Request); var twitterCredentials = await user.GetAppServiceIdentityAsync<TwitterCredentials>(Request); string first = string.Empty, last = string.Empty, profile = string.Empty; userId = await IdentitiyHelper.FindSidAsync(User, Request); if (fbCredentials?.UserClaims?.Count() > 0) { FillDataFromFacebook(fbCredentials, out first, out last, out profile); } else if (msCredentials?.UserClaims?.Count() > 0) { FillDataFromMS(msCredentials, out first, out last, out profile); } else if (twitterCredentials?.UserClaims?.Count() > 0) { FillDataFromTwitter(twitterCredentials, out first, out last, out profile); } else { return null; } var context = new MyDrivingContext(); var curUser = context.UserProfiles.FirstOrDefault(u => u.UserId == userId); if (curUser == null) { curUser = new MyDriving.DataObjects.UserProfile { UserId = userId, ProfilePictureUri = profile, FirstName = first, LastName = last }; context.UserProfiles.Add(curUser); } else { curUser.FirstName = first; curUser.LastName = last; curUser.ProfilePictureUri = profile; } await context.SaveChangesAsync(); return curUser; }
public async Task<IHttpActionResult> Post(string userId, string deviceName) { var aiTelemetry = new TelemetryClient(); Device device = null; EnsureRegistryManagerInitialized(); MobileAppSettingsDictionary settings = Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); await GetDevices(); int maxDevices = int.Parse(settings["MaxDevices"]); MyDrivingContext context = new MyDrivingContext(); var curUser = context.UserProfiles.FirstOrDefault(user => user.UserId == userId) ?? context.UserProfiles.Add(new MyDriving.DataObjects.UserProfile { Id = Guid.NewGuid().ToString(), UserId = userId }); if (curUser.Devices == null) { curUser.Devices = new List<MyDriving.DataObjects.Device>(); } if (curUser.Devices.Count >= maxDevices) { aiTelemetry.TrackEvent("Max number of devices reached for user = "******"You already have more than the maximum number of devices"); } try { device = await registryManager.GetDeviceAsync(deviceName); } catch (Exception e) { aiTelemetry.TrackException(e); } if (device == null) //device not found { try { device = await registryManager.AddDeviceAsync(new Device(deviceName)); if (device != null) { curUser.Devices.Add(new MyDriving.DataObjects.Device { Name = deviceName }); await context.SaveChangesAsync(); } else //registration failed { aiTelemetry.TrackEvent(String.Format("Registration failed for device {0}", deviceName)); return BadRequest("Error. Cannot register device"); } } catch (Exception e) { aiTelemetry.TrackException(e); aiTelemetry.TrackEvent(String.Format("Registration failed for device {0}", deviceName)); return BadRequest("Device provisioning failed on server with exception " + e.Message); } aiTelemetry.TrackEvent(String.Format("New device {0} registered for user {1}. Total devices: {2}", deviceName, curUser.Id, curUser.Devices.Count)); } return Created("api/provision", device?.Authentication?.SymmetricKey?.PrimaryKey); }