Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. public void ConfigureAuth(IAppBuilder app)
  2. {
  3. // Configure the db context, user manager and signin manager to use a single instance per request
  4. app.CreatePerOwinContext(ApplicationDbContext.Create);
  5. app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
  6. app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
  7.  
  8. // Enable the application to use a cookie to store information for the signed in user
  9. // and to use a cookie to temporarily store information about a user logging in with a third party login provider
  10. // Configure the sign in cookie
  11. app.UseCookieAuthentication(new CookieAuthenticationOptions
  12. {
  13. AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  14. LoginPath = new PathString("/Account/Login"),
  15. Provider = new CookieAuthenticationProvider
  16. {
  17. // Enables the application to validate the security stamp when the user logs in.
  18. // This is a security feature which is used when you change a password or add an external login to your account.
  19. OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
  20. validateInterval: TimeSpan.FromMinutes(30),
  21. regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
  22. }
  23. });
  24. app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
  25.  
  26. // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
  27. app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
  28.  
  29. // Enables the application to remember the second login verification factor such as phone or email.
  30. // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
  31. // This is similar to the RememberMe option when you log in.
  32. app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
  33.  
  34. // Uncomment the following lines to enable logging in with third party login providers
  35. //app.UseMicrosoftAccountAuthentication(
  36. // clientId: "",
  37. // clientSecret: "");
  38.  
  39. //app.UseTwitterAuthentication(
  40. // consumerKey: "",
  41. // consumerSecret: "");
  42.  
  43. var options = new FacebookAuthenticationOptions();
  44. options.Scope.Add("email");
  45. options.Scope.Add("friends_about_me");
  46. options.Scope.Add("friends_photos");
  47. options.AppId = "260407644075446";
  48. options.AppSecret = "20c3e1c03c4e2bcc2efe1eee1aaef24d";
  49. options.Provider = new FacebookAuthenticationProvider()
  50. {
  51. OnAuthenticated = context =>
  52. {
  53.  
  54. var userDetail = context.User;
  55.  
  56. string id = (dynamic)context.Id;
  57.  
  58. string emmail = (dynamic)context.Email;
  59.  
  60. var currentUser = UserManager.FindByName(emmail);
  61. if (currentUser.UserProfile == null)
  62. {
  63. currentUser.EmailConfirmed = true;
  64.  
  65. try
  66. {
  67. currentUser.UserProfile = new UserProfile
  68. {
  69. UserProfileId = currentUser.ToString(),
  70. Avatar = ConvertImageURLToBase64(@"https://graph.facebook.com/" + id + "/picture?type=large"),
  71. LastName = ((dynamic)context.User).first_name.Value,
  72. FirstName = ((dynamic)context.User).last_name.Value,
  73. MemberSince = DateTime.Now.Date,
  74. ProfileVisibility = "Private",
  75. ZipCode = "0",
  76. };
  77. UserManager.Update(currentUser);
  78. }
  79. catch (Exception ex)
  80. {
  81. string x = ex.StackTrace.ToString();
  82. }
  83. }
  84. return System.Threading.Tasks.Task.FromResult(0);
  85. }
  86. };
  87. app.UseFacebookAuthentication(options);
  88.  
  89.  
  90. app.UseGooglePlusAuthentication(new GooglePlusAuthenticationOptions()
  91. {
  92. ClientId = "118180854859-i07363vsqsbjbqkmlfsr6cktgoqosd9q.apps.googleusercontent.com",
  93. ClientSecret = "XmYyNO5Xa1TuZPPFe5HnZCe7",
  94. Provider = new GooglePlusAuthenticationProvider()
  95. {
  96.  
  97. OnAuthenticated = context =>
  98. {
  99. var userDetail = context.Person;
  100. context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Identity.FindFirstValue(ClaimTypes.Name)));
  101. context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Identity.FindFirstValue(ClaimTypes.Email)));
  102. string id = ((dynamic)context.Person).id;
  103. string emmail = ((dynamic)context.Person).emails[0].value.Value;
  104. var currentUser = UserManager.FindByName(emmail);
  105. if (currentUser.UserProfile == null)
  106. {
  107. currentUser.EmailConfirmed = true;
  108.  
  109. currentUser.UserProfile = new UserProfile
  110. {
  111. UserProfileId = currentUser.ToString(),
  112. Avatar = ConvertImageURLToBase64(((dynamic)context.Person).image.url.Value),
  113. LastName = ((dynamic)context.Person).name.familyName.Value,
  114. FirstName = ((dynamic)context.Person).name.givenName.Value,
  115. MemberSince = DateTime.Now.Date,
  116. ProfileVisibility = "Private",
  117. ZipCode = "0"
  118. };
  119. UserManager.Update(currentUser);
  120. }
  121.  
  122. return System.Threading.Tasks.Task.FromResult(0);
  123. },
  124. },
  125. });
  126. }
  127.  
  128. [AllowAnonymous]
  129. public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
  130. {
  131. var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
  132. if (loginInfo == null)
  133. {
  134. return RedirectToAction("Login");
  135. }
  136.  
  137. // Sign in the user with this external login provider if the user already has a login
  138. var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
  139. switch (result)
  140. {
  141. case SignInStatus.Success:
  142. return RedirectToLocal(returnUrl);
  143. case SignInStatus.LockedOut:
  144. return View("Lockout");
  145. case SignInStatus.RequiresVerification:
  146. return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
  147. case SignInStatus.Failure:
  148. default:
  149. // If the user does not have an account, then prompt the user to create an account
  150. ViewBag.ReturnUrl = returnUrl;
  151. ViewBag.L`enter code here`oginProvider = loginInfo.Login.LoginProvider;
  152. return View("ExternalLoginConfirmation", new ExternalLogi![enter image description here][1]nConfirmationViewModel { Email = loginInfo.Email });
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement