Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. public partial class Startup
  2. {
  3. // O armazenamento de dados que usamos para salvar o token de acesso do usuário depois de fazer login.
  4. private IDataStore dataStore = new FileDataStore(GoogleWebAuthorizationBroker.Folder);
  5.  
  6. public void ConfigureAuth(IAppBuilder app)
  7. {
  8. // Configure o contexto do banco de dados, o gerenciador de usuários e o gerenciador de login para usar uma única instância por solicitação
  9. app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
  10.  
  11. // Habilitar o aplicativo para usar um cookie para armazenar informações para o usuário conectado e usar um cookie para armazenar temporariamente informações sobre um usuário efetuando login com um provedor de login de terceiros. Configurar o cookie de login
  12. app.UseCookieAuthentication(new CookieAuthenticationOptions
  13. {
  14. AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  15. LoginPath = new PathString("/seguro/acesso"),
  16. Provider = new CookieAuthenticationProvider
  17. {
  18. // Permite que o aplicativo valide o carimbo de segurança quando o usuário faz o login. Esse é um recurso de segurança que é usado quando você altera uma senha ou adiciona um login externo à sua conta.
  19. OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
  20. validateInterval: TimeSpan.FromHours(8),
  21. regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
  22. }
  23. });
  24.  
  25. //...
  26.  
  27. // Permite o login com o provedor de login do Google.
  28. var google = new GoogleOAuth2AuthenticationOptions()
  29. {
  30. AccessType = "offline",
  31. ClientId = ConfigurationManager.AppSettings["google-clientid"],
  32. ClientSecret = ConfigurationManager.AppSettings["google-clientsecret"],
  33. Provider = new GoogleOAuth2AuthenticationProvider()
  34. {
  35. OnAuthenticated = async context =>
  36. {
  37. var userId = context.Id;
  38. context.Identity.AddClaim(new Claim(MyClaimTypes.GoogleUserId, userId));
  39.  
  40. var tokenResponse = new TokenResponse()
  41. {
  42. AccessToken = context.AccessToken,
  43. RefreshToken = context.RefreshToken,
  44. ExpiresInSeconds = (long)context.ExpiresIn.Value.TotalSeconds,
  45. Issued = DateTime.Now,
  46. };
  47.  
  48. await dataStore.StoreAsync(userId, tokenResponse);
  49. },
  50. },
  51. };
  52.  
  53. foreach (var scope in MyRequestedScopes.Scopes)
  54. {
  55. google.Scope.Add(scope);
  56. }
  57.  
  58. app.UseGoogleAuthentication(google);
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement