Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authentication;
- using Microsoft.AspNetCore.Authentication.Cookies;
- using Microsoft.AspNetCore.Mvc;
- namespace GCalendar.SimpleLogin.Controllers
- {
- public class AccountController : Controller
- {
- [HttpGet]
- public IActionResult Login()
- {
- return View();
- }
- public IActionResult AccessDenied()
- {
- return Content("Access Denied");
- }
- [HttpGet]
- public IActionResult ExternalLogin(string returnUrl = null)
- {
- var properties = new AuthenticationProperties
- {
- RedirectUri = returnUrl ?? "/Home/About"
- };
- return Challenge(properties);
- }
- public async Task<IActionResult> Logout()
- {
- await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
- return RedirectToAction(nameof(HomeController.Index), "Home");
- }
- private IActionResult GoToReturnUrl(string returnUrl)
- {
- if (Url.IsLocalUrl(returnUrl))
- {
- return Redirect(returnUrl);
- }
- return RedirectToAction("Index", "Home");
- }
- }
- }
- if (User.Identity.IsAuthenticated)
- {
- string accessToken = await HttpContext.GetTokenAsync("access_token");
- string idToken = await HttpContext.GetTokenAsync("id_token");
- // Now you can use them. For more info on when and how to use the
- // access_token and id_token, see https://auth0.com/docs/tokens
- }
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddAuthentication(options =>
- {
- options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
- options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
- options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
- })
- .AddCookie()
- .AddGoogle(options =>
- {
- options.ClientId = Configuration["Authentication:Google:ClientId"];
- options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
- options.Scope.Add("https://www.googleapis.com/auth/calendar");
- options.SaveTokens = true;
- });
- services.AddMvc();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement