public async Task <IHttpActionResult> Post(string userId, string deviceName) { 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) { return(BadRequest("You already have more than the maximum number of devices")); } try { device = await registryManager.GetDeviceAsync(deviceName); } catch (Exception) { } 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 { return(BadRequest("Error. Cannot register device")); } } catch (Exception e) { return(BadRequest("Device provisioning failed on server with exception " + e.Message)); } } return(Created("api/provision", device?.Authentication?.SymmetricKey?.PrimaryKey)); }
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); }