Advertisement
Guest User

Untitled

a guest
May 24th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. public void ConfigureOAuth(IAppBuilder app)
  2. {
  3. //use a cookie to temporarily store information about a user logging in with a third party login provider
  4. app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
  5. OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
  6.  
  7. OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
  8. {
  9. AllowInsecureHttp = true,
  10. TokenEndpointPath = new PathString("/token"),
  11. AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
  12. Provider = new SimpleAuthorizationServerProvider(),
  13. RefreshTokenProvider = new SimpleRefreshTokenProvider(),
  14. AuthenticationMode = AuthenticationMode.Active
  15. };
  16.  
  17. // Token Generation
  18. app.UseOAuthAuthorizationServer(OAuthServerOptions);
  19. app.UseOAuthBearerAuthentication(OAuthBearerOptions);
  20.  
  21. }
  22.  
  23. public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
  24. {
  25.  
  26. string clientId = string.Empty;
  27. string clientSecret = string.Empty;
  28. Client client = null;
  29.  
  30. if (!context.TryGetBasicCredentials(out clientId, out clientSecret))
  31. {
  32. context.TryGetFormCredentials(out clientId, out clientSecret);
  33. }
  34.  
  35. if (context.ClientId == null)
  36. {
  37. //Remove the comments from the below line context.SetError, and invalidate context
  38. //if you want to force sending clientId/secrects once obtain access tokens.
  39. context.Validated();
  40. //context.SetError("invalid_clientId", "ClientId should be sent.");
  41. return Task.FromResult<object>(null);
  42. }
  43.  
  44. using (AuthRepository _repo = new AuthRepository())
  45. {
  46. client = _repo.FindClient(context.ClientId);
  47. }
  48.  
  49. if (client == null)
  50. {
  51. context.SetError("invalid_clientId", string.Format("Client '{0}' is not registered in the system.", context.ClientId));
  52. return Task.FromResult<object>(null);
  53. }
  54.  
  55. if (client.ApplicationType == Models.ApplicationTypes.NativeConfidential)
  56. {
  57. if (string.IsNullOrWhiteSpace(clientSecret))
  58. {
  59. context.SetError("invalid_clientId", "Client secret should be sent.");
  60. return Task.FromResult<object>(null);
  61. }
  62. else
  63. {
  64. if (client.Secret != Helper.GetHash(clientSecret))
  65. {
  66. context.SetError("invalid_clientId", "Client secret is invalid.");
  67. return Task.FromResult<object>(null);
  68. }
  69. }
  70. }
  71.  
  72. if (!client.Active)
  73. {
  74. context.SetError("invalid_clientId", "Client is inactive.");
  75. return Task.FromResult<object>(null);
  76. }
  77.  
  78. context.OwinContext.Set<string>("as:clientAllowedOrigin", client.AllowedOrigin);
  79. context.OwinContext.Set<string>("as:clientRefreshTokenLifeTime", client.RefreshTokenLifeTime.ToString());
  80.  
  81. context.Validated();
  82. return Task.FromResult<object>(null);
  83. }
  84.  
  85. var data = "grant_type=password&username=" + loginData.username + "&password=" + loginData.password;
  86.  
  87. var deferred = $q.defer();
  88.  
  89. $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {
  90.  
  91. if (loginData.useRefreshTokens) {
  92. localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName, refreshToken: response.refresh_token, useRefreshTokens: true });
  93. }
  94. else {
  95. localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName, refreshToken: "", useRefreshTokens: false });
  96. $rootScope.authData = { token: response.access_token, userName: loginData.userName, refreshToken: "", useRefreshTokens: false };
  97. }
  98. _authentication.isAuth = true;
  99. _authentication.userName = loginData.userName;
  100. _authentication.useRefreshTokens = loginData.useRefreshTokens;
  101.  
  102. deferred.resolve(response);
  103.  
  104. }).error(function (err, status) {
  105. _logOut();
  106. deferred.reject(err);
  107. });
  108.  
  109. return deferred.promise;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement