public ActionResult Create(JournalEntry journalentry) { if (ModelState.IsValid) { // TODO use an Azure Queue that will be monitored by a worker role //add the custom type for health vault ItemTypeManager.RegisterTypeHandler(HVJournalEntry.TypeId, typeof(HVJournalEntry), true); // get the authed user var authorizedUser = (User as HVPrincipal); if (authorizedUser != null) { //get the auth token var authToken = authorizedUser.AuthToken; // create the appropriate objects for health vault var appId = HealthApplicationConfiguration.Current.ApplicationId; WebApplicationCredential cred = new WebApplicationCredential( appId, authToken, HealthApplicationConfiguration.Current.ApplicationCertificate); // setup the user WebApplicationConnection connection = new WebApplicationConnection(appId, cred); PersonInfo personInfo = HealthVaultPlatform.GetPersonInfo(connection); // before we add make sure we still have permission to add var result = personInfo.SelectedRecord.QueryPermissionsByTypes(new List<Guid>() { HVJournalEntry.TypeId }).FirstOrDefault(); if (!result.Value.OnlineAccessPermissions.HasFlag(HealthRecordItemPermissions.Create)) throw new ArgumentNullException("unable to create record as no permission is given from health vault"); //Now add to the HV system personInfo.SelectedRecord.NewItem(new HVJournalEntry(journalentry)); // redirect return RedirectToAction("Index"); } } return View(journalentry); }
public ActionResult Login(LoginModel model, string returnUrl) { // here we are getting posted from HealthVault so extract the wctoken sent string authToken = Request.Params["wctoken"]; if (authToken != null) { // create a web app cred object var appId = HealthApplicationConfiguration.Current.ApplicationId; WebApplicationCredential cred = new WebApplicationCredential( appId, authToken, HealthApplicationConfiguration.Current.ApplicationCertificate); // setup the user WebApplicationConnection connection = new WebApplicationConnection(appId, cred); PersonInfo personInfo = HealthVaultPlatform.GetPersonInfo(connection); // check to make sure there is access to records if (personInfo.AuthorizedRecords.Count() == 0) throw new Exception("There are no authorized users for us to work with!"); // check to see if the user exists var personId = personInfo.PersonId.ToString(); // we found the user so authenticate them var username = personId; var password = personId + appId; if (Membership.ValidateUser(username, password)) { // user has authenticated var user = Membership.GetUser(personInfo.PersonId.ToString()); // save auth cookie CreateAuthCookie(personInfo, user, authToken); } else { // the user has not registered with us so create one // Attempt to register the user MembershipCreateStatus createStatus; var newUser = Membership.CreateUser(username, password, "", passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus); if (createStatus == MembershipCreateStatus.Success) { //save auth cookie CreateAuthCookie(personInfo, newUser, authToken); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); return View(model); } } // save the user to the local table SaveUser(personInfo, authToken); // save the user avatar image to blob HVUserImageHelper.Default.SaveImageToBlobStorage(personInfo.SelectedRecord == null ? personInfo.AuthorizedRecords.FirstOrDefault().Value : personInfo.SelectedRecord); // redirect to the actionqs NameValueCollection query = HttpUtility.ParseQueryString(Request.Url.Query); var r = HttpUtility.UrlDecode(query["actionqs"]); return Redirect(new Uri(string.Format("http://{0}{1}{2}", Request.Url.Host, (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port), r)).ToString()); } else { // no wctoken so just redirect to home ModelState.AddModelError("", "Unable to authenticate with Microsoft HealthVault."); } // If we got this far, something failed, redisplay form return View(model); }
/// <summary> /// Get's the authenticated person's information using the specified authentication token. /// </summary> /// /// <param name="authToken"> /// The authentication token for a user. This can be retrieved by extracting the WCToken /// query string parameter from the request after the user has been redirected to the /// HealthVault AUTH page. See <see cref="RedirectToShellUrl(HttpContext, string)"/> for more information. /// </param> /// /// <param name="appId"> /// The unique identifier for the application. /// </param> /// /// <returns> /// The information about the logged in person. /// </returns> /// public static PersonInfo GetPersonInfo(string authToken, Guid appId) { WebApplicationCredential cred = new WebApplicationCredential( appId, authToken, HealthApplicationConfiguration.Current.ApplicationCertificate); // set up our cookie WebApplicationConnection connection = new WebApplicationConnection(appId, cred); PersonInfo personInfo = HealthVaultPlatform.GetPersonInfo(connection); personInfo.ApplicationSettingsChanged += new EventHandler(OnPersonInfoChanged); personInfo.SelectedRecordChanged += new EventHandler(OnPersonInfoChanged); return personInfo; }
// // GET: /JournalEntry/Delete/5 public ActionResult Delete(string id) { // create the item key var t = id.Split(','); var key = new HealthRecordItemKey(Guid.Parse(t[0]), Guid.Parse(t[1])); // get the user var hvUser = (User as HVPrincipal); if (hvUser != null) { // get the auth token var authToken = hvUser.AuthToken; // create the appropriate objects for health vault var appId = HealthApplicationConfiguration.Current.ApplicationId; WebApplicationCredential cred = new WebApplicationCredential( appId, authToken, HealthApplicationConfiguration.Current.ApplicationCertificate); // setup the user WebApplicationConnection connection = new WebApplicationConnection(appId, cred); PersonInfo personInfo = null; personInfo = HealthVaultPlatform.GetPersonInfo(connection); // delete the record personInfo.SelectedRecord.RemoveItem(key); } // redirect return RedirectToAction("Index"); }
// // GET: /JournalEntry/ public ActionResult Index() { // register the custom type ItemTypeManager.RegisterTypeHandler(HVJournalEntry.TypeId, typeof(HVJournalEntry), true); // get the user var hvUser = (User as HVPrincipal); if (hvUser != null) { // get the auth token var authToken = hvUser.AuthToken; // create the appropriate objects for health vault var appId = HealthApplicationConfiguration.Current.ApplicationId; WebApplicationCredential cred = new WebApplicationCredential( appId, authToken, HealthApplicationConfiguration.Current.ApplicationCertificate); // setup the user WebApplicationConnection connection = new WebApplicationConnection(appId, cred); PersonInfo personInfo = null; personInfo = HealthVaultPlatform.GetPersonInfo(connection); // before we add make sure we still have permission to add var result = personInfo.SelectedRecord.QueryPermissionsByTypes(new List<Guid>() { HVJournalEntry.TypeId }).FirstOrDefault(); if (!result.Value.OnlineAccessPermissions.HasFlag(HealthRecordItemPermissions.Read)) throw new ArgumentNullException("unable to create record as no permission is given from health vault"); // search hv for the records HealthRecordSearcher searcher = personInfo.SelectedRecord.CreateSearcher(); HealthRecordFilter filter = new HealthRecordFilter(HVJournalEntry.TypeId); searcher.Filters.Add(filter); // get the matching items HealthRecordItemCollection entries = searcher.GetMatchingItems()[0]; // compile a list of journalEntryItems only var items = entries.Cast<HVJournalEntry>().ToList(); var ret = new List<JournalEntry>(items.Count()); foreach (var t in items) { var je = t.JournalEntry; je.HvId = t.Key.ToString(); ret.Add(je); } // return the list to the view return View(ret); } else { // if we make it here there is nothing to display return View(new List<JournalEntry>(0)); } }
public ActionResult GetUserData(int userId = -1) { // just do a basic check if (userId == -1) return Json(new { status = "error", msg = "userId not sent" }, JsonRequestBehavior.AllowGet); // try to find the user var context = new HVDbContext(); var user = (from t in context.HealthVaultUsers where t.Id == userId select t).FirstOrDefault(); // if no user is found return error if (user == null) return Json(new { status = "error", msg = "userId not found" }, JsonRequestBehavior.AllowGet); // extract the token and make the request to health vault for all the data var authToken = user.WCToken; // register the type in the HV SDK ItemTypeManager.RegisterTypeHandler(HVJournalEntry.TypeId, typeof(HVJournalEntry), true); // create the appropriate objects for health vault var appId = HealthApplicationConfiguration.Current.ApplicationId; WebApplicationCredential cred = new WebApplicationCredential( appId, authToken, HealthApplicationConfiguration.Current.ApplicationCertificate); // setup the user WebApplicationConnection connection = new WebApplicationConnection(appId, cred); PersonInfo personInfo = null; try { personInfo = HealthVaultPlatform.GetPersonInfo(connection); } catch { return Json(new { status = "error", msg = "Unable to connect to HealthVault service" }, JsonRequestBehavior.AllowGet); } // get the selected record var authRecord = personInfo.SelectedRecord; // make sure there is a record returned if (authRecord == null) return Json(new { status = "error", msg = "cannot get selected record" }, JsonRequestBehavior.AllowGet); // before we add make sure we still have permission to read var result = authRecord.QueryPermissionsByTypes(new List<Guid>() { HVJournalEntry.TypeId }).FirstOrDefault(); if (!result.Value.OnlineAccessPermissions.HasFlag(HealthRecordItemPermissions.Read)) return Json(new { status = "error", msg = "unable to create record as no permission is given from health vault" }, JsonRequestBehavior.AllowGet); // search hv for the records HealthRecordSearcher searcher = authRecord.CreateSearcher(); HealthRecordFilter filter = new HealthRecordFilter(HVJournalEntry.TypeId); searcher.Filters.Add(filter); HealthRecordItemCollection entries = searcher.GetMatchingItems()[0]; var ret = entries.Cast<HVJournalEntry>().ToList().Select(t => t.JournalEntry); return Json(new { status = "ok", data = ret }, JsonRequestBehavior.AllowGet); }
internal static WebApplicationConnection GetConnection(string wcToken) { WebApplicationConnection connection = new WebApplicationConnection(WebApplicationConfiguration.AppId, WebApplicationConfiguration.HealthServiceUrl, new WebApplicationCredential(WebApplicationConfiguration.AppId, wcToken)); connection.Authenticate(); return connection; }