public async Task <IActionResult> VerifyLinkId([FromBody] VerifyLinkIdModel model) { IActionResult response = Unauthorized(); var manager = _context.ManagerFactory.CreateSurveyLinkManager(); var link = await manager.GetLink(model.LinkId); if (link != null) // if link is verified { var claims = new List <Claim> { new Claim(ClaimTypes.Role, Role.Volunteer.ToString()), new Claim(ClaimTypes.NameIdentifier, link.ParticipantId.ToString()), new Claim(ClaimTypes.Uri, link.LinkId), new Claim(ClaimTypes.Name, link.SurveyName) }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); response = Ok(new { message = link.SurveyName }); //Read response in view and redirect to survey url } return(response); }
public async Task SignInAsVolunteer(string surveyName, string linkId, string participantEmail, int participantId) { var linkModel = new SurveyLinkModel() { LinkId = linkId, SurveyName = surveyName, ParticipantId = participantId, ParticipantEmail = participantEmail }; var manager = ManagerFactory.CreateSurveyLinkManager(); await manager.SaveSurveyLink(linkModel); var verifyLinkIdModel = new VerifyLinkIdModel() { LinkId = linkModel.LinkId }; var content = JsonConvert.SerializeObject(verifyLinkIdModel); var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri("Cookie/VerifyLinkId", UriKind.Relative), Content = new StringContent(content, Encoding.UTF8, "application/json") }; var response = await HttpClient.SendAsync(request); response.EnsureSuccessStatusCode(); }
public async Task CookieController_VerifyLinkIdThatExists_CookieIsReturned() { var participantEmail = "*****@*****.**"; var id = 1; var surveyName = "TestSurvey"; var linkModel = new SurveyLinkModel() { SurveyName = surveyName, ParticipantId = id, ParticipantEmail = participantEmail }; var linkId = linkModel.LinkId; var linkManager = _fixture.ManagerFactory.CreateSurveyLinkManager(); await linkManager.SaveSurveyLink(linkModel); var verifyLinkIdModel = new VerifyLinkIdModel() { LinkId = linkId }; var content = JsonConvert.SerializeObject(verifyLinkIdModel); var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri("Cookie/VerifyLinkId", UriKind.Relative), Content = new StringContent(content, Encoding.UTF8, "application/json") }; var response = await _fixture.HttpClient.SendAsync(request); response.Headers.TryGetValues("Set-Cookie", out var newCookies); var cookieList = newCookies.ToList(); Assert.Single(cookieList); Assert.Contains("auth_cookie", cookieList.First()); }
public async Task CookieController_VerifyLinkIdThatDoesNotExist_NoCookieIsReturned() { var verifyLinkIdModel = new VerifyLinkIdModel() { LinkId = "TestId" }; var content = JsonConvert.SerializeObject(verifyLinkIdModel); var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri("Cookie/VerifyLinkId", UriKind.Relative), Content = new StringContent(content, Encoding.UTF8, "application/json") }; var response = await _fixture.HttpClient.SendAsync(request); response.Headers.TryGetValues("Set-Cookie", out var newCookies); Assert.Null(newCookies); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }