Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Authentication;
  7. using Microsoft.AspNetCore.Authentication.Cookies;
  8. using Microsoft.AspNetCore.Mvc;
  9.  
  10. namespace GCalendar.SimpleLogin.Controllers
  11. {
  12. public class AccountController : Controller
  13. {
  14. [HttpGet]
  15. public IActionResult Login()
  16. {
  17. return View();
  18. }
  19.  
  20. public IActionResult AccessDenied()
  21. {
  22. return Content("Access Denied");
  23. }
  24.  
  25. [HttpGet]
  26. public IActionResult ExternalLogin(string returnUrl = null)
  27. {
  28. var properties = new AuthenticationProperties
  29. {
  30. RedirectUri = returnUrl ?? "/Home/About"
  31. };
  32.  
  33. return Challenge(properties);
  34. }
  35.  
  36.  
  37.  
  38. public async Task<IActionResult> Logout()
  39. {
  40. await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
  41.  
  42. return RedirectToAction(nameof(HomeController.Index), "Home");
  43. }
  44.  
  45.  
  46. private IActionResult GoToReturnUrl(string returnUrl)
  47. {
  48. if (Url.IsLocalUrl(returnUrl))
  49. {
  50. return Redirect(returnUrl);
  51. }
  52. return RedirectToAction("Index", "Home");
  53. }
  54. }
  55. }
  56.  
  57.  
  58. if (User.Identity.IsAuthenticated)
  59. {
  60. string accessToken = await HttpContext.GetTokenAsync("access_token");
  61. string idToken = await HttpContext.GetTokenAsync("id_token");
  62.  
  63. // Now you can use them. For more info on when and how to use the
  64. // access_token and id_token, see https://auth0.com/docs/tokens
  65. }
  66.  
  67. public void ConfigureServices(IServiceCollection services)
  68. {
  69. services.AddAuthentication(options =>
  70. {
  71. options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  72. options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  73. options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
  74. })
  75. .AddCookie()
  76. .AddGoogle(options =>
  77. {
  78. options.ClientId = Configuration["Authentication:Google:ClientId"];
  79. options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
  80. options.Scope.Add("https://www.googleapis.com/auth/calendar");
  81. options.SaveTokens = true;
  82. });
  83.  
  84. services.AddMvc();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement