Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 using System; | |
2 using Microsoft.AspNet.Identity; | |
3 using Microsoft.AspNet.Identity.Owin; | |
4 using Microsoft.Owin; | |
5 using Microsoft.Owin.Security.Cookies; | |
6 using Microsoft.Owin.Security.Google; | |
7 using Owin; | |
8 using Calendar.ASP.NET.MVC5.Models; | |
9 using System.Security.Claims; | |
class
2015/02/23 17:08:34
Personally, I group together System.*, 1P.*, 3P.*
jmcgrew
2015/02/28 01:33:41
Done.
| |
10 using System.Threading.Tasks; | |
11 | |
12 namespace Calendar.ASP.NET.MVC5 | |
13 { | |
14 public partial class Startup | |
peleyal
2015/01/12 08:11:24
again... what did you change in this class?
It wil
jmcgrew
2015/02/20 02:53:17
Done.
| |
15 { | |
16 // For more information on configuring authentication, please visit http ://go.microsoft.com/fwlink/?LinkId=301864 | |
17 public void ConfigureAuth(IAppBuilder app) | |
18 { | |
19 // Configure the db context, user manager and signin manager to use a single instance per request | |
20 app.CreatePerOwinContext(ApplicationDbContext.Create); | |
21 app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserMana ger.Create); | |
22 app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignIn Manager.Create); | |
23 | |
24 // Enable the application to use a cookie to store information for t he signed in user | |
25 // and to use a cookie to temporarily store information about a user logging in with a third party login provider | |
26 // Configure the sign in cookie | |
27 app.UseCookieAuthentication(new CookieAuthenticationOptions | |
28 { | |
29 AuthenticationType = DefaultAuthenticationTypes.ApplicationCooki e, | |
30 LoginPath = new PathString("/Account/Login"), | |
31 Provider = new CookieAuthenticationProvider | |
32 { | |
33 // Enables the application to validate the security stamp wh en the user logs in. | |
34 // This is a security feature which is used when you change a password or add an external login to your account.·· | |
35 OnValidateIdentity = SecurityStampValidator.OnValidateIdenti ty<ApplicationUserManager, ApplicationUser>( | |
36 validateInterval: TimeSpan.FromMinutes(30), | |
37 regenerateIdentity: (manager, user) => user.GenerateUser IdentityAsync(manager)) | |
38 } | |
39 });············ | |
40 app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCooki e); | |
41 | |
42 // Enables the application to temporarily store user information whe n they are verifying the second factor in the two-factor authentication process. | |
43 app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCoo kie, TimeSpan.FromMinutes(5)); | |
44 | |
45 // Enables the application to remember the second login verification factor such as phone or email. | |
46 // Once you check this option, your second step of verification duri ng the login process will be remembered on the device where you logged in from. | |
47 // This is similar to the RememberMe option when you log in. | |
48 app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.Two FactorRememberBrowserCookie); | |
49 | |
50 // Uncomment the following lines to enable logging in with third par ty login providers | |
51 //app.UseMicrosoftAccountAuthentication( | |
52 // clientId: "", | |
53 // clientSecret: ""); | |
54 | |
55 //app.UseTwitterAuthentication( | |
56 // consumerKey: "", | |
57 // consumerSecret: ""); | |
58 | |
59 //app.UseFacebookAuthentication( | |
60 // appId: "", | |
61 // appSecret: ""); | |
62 | |
63 var google = new GoogleOAuth2AuthenticationOptions() | |
64 { | |
65 AccessType = "offline", // Request a refresh token. | |
66 ClientId = MyClientSecrets.ClientId, | |
67 ClientSecret = MyClientSecrets.ClientSecret, | |
68 Provider = new GoogleOAuth2AuthenticationProvider() | |
69 { | |
70 OnAuthenticated = async context => | |
71 { | |
72 context.Identity.AddClaim(new Claim(MyClaimTypes.GoogleA ccessToken, context.AccessToken)); | |
73 if (context.RefreshToken != null) | |
74 { | |
75 context.Identity.AddClaim(new Claim(MyClaimTypes.Goo gleRefreshToken, context.RefreshToken)); | |
76 } | |
77 context.Identity.AddClaim(new Claim(MyClaimTypes.GoogleU serId, context.Id)); | |
78 var expiresAt = DateTime.UtcNow + context.ExpiresIn; | |
79 context.Identity.AddClaim(new Claim(MyClaimTypes.TokenEx piresAt, expiresAt.Value.ToBinary().ToString())); | |
80 await Task.Delay(0); | |
81 }, | |
82 }, | |
83 }; | |
84 | |
85 foreach (var scope in MyRequestedScopes.GetScopes()) | |
86 { | |
87 google.Scope.Add(scope); | |
88 } | |
89 | |
90 app.UseGoogleAuthentication(google); | |
91 } | |
92 } | |
93 } | |
OLD | NEW |