public void Post([FromQuery] string id) { // Passed the SessionID from the View which we store against the UserId for later use. var userId = UserId.GetUserId(HttpContext, this.User); Sessions[userId] = id; }
/// <summary> /// Retrieve a URL for the user to link a given connection name to their Bot /// </summary> /// <param name="account"></param> /// <returns></returns> public async Task <IActionResult> SignIn(TokenStatus account) { var userId = UserId.GetUserId(HttpContext, this.User); string link = await repository.GetSignInLinkAsync(userId, CredentialProvider, account.ConnectionName, $"{this.Request.Scheme}://{this.Request.Host.Value}/Home/LinkedAccounts"); return(Redirect(link)); }
public async Task <IActionResult> SignOutAll() { var userId = UserId.GetUserId(HttpContext, this.User); await this.repository.SignOutAsync(userId, CredentialProvider); return(RedirectToAction("LinkedAccounts")); }
public async Task <IActionResult> SignOutAll(bool companionApp) { var userId = UserId.GetUserId(this.HttpContext, this.User); await this.repository.SignOutAsync(userId, this.CredentialProvider); return(this.RedirectToAction("LinkedAccounts", new { companionApp })); }
/// <summary> /// Retrieve a URL for the user to link a given connection name to their Bot. /// </summary> /// <param name="connectionName">Connection Name.</param> /// <param name="companionApp">From companion app.</param> /// <returns>IActionResult.</returns> public async Task <IActionResult> SignIn(string connectionName, bool companionApp) { var userId = UserId.GetUserId(this.HttpContext, this.User); string link = await this.repository.GetSignInLinkAsync(userId, this.CredentialProvider, connectionName, $"{this.Request.Scheme}://{this.Request.Host.Value}/Home/LinkedAccounts?companionApp={companionApp.ToString()}"); return(this.Redirect(link)); }
/// <summary> /// Sign a user out of a given connection name previously linked to their Bot. /// </summary> /// <param name="account">TokenStatus information.</param> /// <returns>IActionResult.</returns> public async Task <IActionResult> SignOut(TokenStatus account) { var userId = UserId.GetUserId(this.HttpContext, this.User); await this.repository.SignOutAsync(userId, this.CredentialProvider, account.ConnectionName); return(this.RedirectToAction("LinkedAccounts")); }
/// <summary> /// Initialisation work for the Linked Accounts feature. /// </summary> /// <param name="companionApp">Flag used to show a sample deep link to a companion application.</param> /// <returns>IActionResult.</returns> public async Task <IActionResult> LinkedAccounts(bool companionApp = false) { this.ViewData["Message"] = "Your application description page."; this.HttpContext.Session.Set("companionApp", BitConverter.GetBytes(companionApp)); var secret = this.Configuration.GetSection("DirectLineSecret")?.Value; var endpoint = this.Configuration.GetSection("DirectLineEndpoint")?.Value; // First step is to exchange the DirectLine Secret for a Token HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"{endpoint}/tokens/generate"); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", secret); // In order to avoid magic code prompts we need to set a TrustedOrigin, therefore requests using the token can be validated // as coming from this web-site and protecting against scenarios where a URL is shared with someone else string trustedOrigin = $"{this.HttpContext.Request.Scheme}://{this.HttpContext.Request.Host}"; request.Content = new StringContent( JsonConvert.SerializeObject(new { TrustedOrigins = new string[] { trustedOrigin } }), Encoding.UTF8, "application/json"); var response = await client.SendAsync(request); string token = string.Empty; if (response.IsSuccessStatusCode) { // We have a Directline Token var body = await response.Content.ReadAsStringAsync(); token = JsonConvert.DeserializeObject <DirectLineToken>(body).token; var userId = UserId.GetUserId(this.HttpContext, this.User); this.HttpContext.Session.SetString("userId", userId); // Retrieve the status TokenStatus[] tokenStatuses = await this.repository.GetTokenStatusAsync(userId, this.CredentialProvider); // Pass the DirectLine Token, Endpont and Token Status to the View model return(this.View(new LinkedAccountsViewModel() { UserId = userId, DirectLineToken = token, Endpoint = endpoint, Status = tokenStatuses, CompanionApp = companionApp, })); } else { throw new InvalidOperationException($"Exchanging a DirectLine Secret for a Token failed, check your configuration settings. Error: {response.ReasonPhrase}"); } }