Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. public class TestAuthentication
  2. {
  3. private HttpClient _client;
  4.  
  5. public TestAuthentication()
  6. {
  7. _client = new HttpClient();
  8. }
  9. public async Task RunTest()
  10. {
  11. var token = await GetToken();
  12. if (string.IsNullOrWhiteSpace(token)) return;
  13. await GetClaims(token);
  14.  
  15. }
  16.  
  17. private async Task<string> GetToken()
  18. {
  19. var response = "";
  20. var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
  21.  
  22. //var tokenClient = new TokenClient(disco.TokenEndpoint, "EduOne", "secret");
  23. //var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api");
  24.  
  25.  
  26. var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret");
  27. var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("alice@mail.com", "Password1!", "api1");
  28. // var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("alice@mail.com", "Password1!", "openid");
  29. if (tokenResponse.IsError)
  30. {
  31. Console.Out.WriteLine("Error:");
  32. Console.Out.WriteLine(tokenResponse.Error);
  33. Console.Out.Write(tokenResponse.ErrorDescription);
  34.  
  35. }
  36. else
  37. {
  38. var extraClaims = new UserInfoClient(disco.UserInfoEndpoint);
  39. var identityClaims = await extraClaims.GetAsync(tokenResponse.AccessToken);
  40. response = tokenResponse.Json.ToString();
  41. Console.Out.WriteLine($"token: {response}");
  42.  
  43. }
  44. return response;
  45. }
  46. private async Task GetClaims(string token)
  47. {
  48. try
  49. {
  50. var obj = JObject.Parse(token);
  51. var tok = obj["access_token"]?.ToString();
  52. _client = new HttpClient();
  53. _client.SetBearerToken(tok);
  54. var response = await _client.GetAsync("http://localhost:5000/api/v1/identity");
  55. if (!response.IsSuccessStatusCode)
  56. {
  57. Console.WriteLine(response.StatusCode);
  58. }
  59. else
  60. {
  61. var content = await response.Content.ReadAsStringAsync();
  62. Console.WriteLine(JArray.Parse(content));
  63. }
  64. }
  65. catch (Exception e)
  66. {
  67. var m = e.Message;
  68. //throw;
  69. }
  70.  
  71. }
  72. ~TestAuthentication()
  73. {
  74.  
  75. _client = null;
  76. }
  77. }
  78.  
  79. new Client
  80. {
  81. ClientId = "ro.client",
  82. AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
  83. ClientSecrets =
  84. {
  85. new Secret("secret".Sha256())
  86. },
  87.  
  88. AllowedScopes = {"api1" },
  89. AccessTokenType = AccessTokenType.Reference
  90. },
  91.  
  92. new TestUser
  93. {
  94. SubjectId = "1",
  95. Username = "alice@mail.com",
  96. Password = "Password1!",
  97. Claims =
  98. {
  99. new Claim(JwtClaimTypes.Email, "mail@mail.com")
  100. }
  101. },
  102.  
  103. new IdentityResource("api1", new string[]{JwtClaimTypes.Email})
  104.  
  105. app.UseIdentityServer();
  106. // app.UseIdentity();
  107.  
  108.  
  109.  
  110.  
  111.  
  112. // app.UseIdentity();
  113.  
  114. app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
  115. {
  116. ApiSecret = "secret",
  117. Authority = "http://localhost:5000",
  118. RequireHttpsMetadata = false,
  119. DiscoveryDocumentRefreshInterval = TimeSpan.FromMinutes(5),
  120.  
  121. ApiName = "FiserOpenIdentityApi",
  122. SupportedTokens = IdentityServer4.AccessTokenValidation.SupportedTokens.Both,
  123. AllowedScopes = { "openid", "profile", "email", "api1", "FiserOpenIdentityApi" }
  124. });
  125. JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
  126. app.UseCookieAuthentication(new CookieAuthenticationOptions
  127. {
  128. AuthenticationScheme = "Cookies"
  129. });
  130. app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
  131. {
  132.  
  133. AuthenticationScheme = "oidc",
  134. SignInScheme = "Cookies",
  135. Authority = "http://localhost:5000",
  136. ClientId = "ro.client",
  137. RequireHttpsMetadata = false,
  138. ClientSecret = "secret",
  139. SaveTokens = false
  140. });
  141. // app.UseJwtBearerAuthentication();
  142.  
  143. app.UseMvc(routes =>
  144. {
  145. routes.MapRoute(
  146. name: "default",
  147. template: "{controller=Home}/{action=Index}/{id?}");
  148. routes.MapRoute(
  149. name: "RESTApiV1",
  150. template: "api/v1/{controller}/{action}/{id?}");
  151.  
  152. });
  153. app.UseMongoDbForIdentityServer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement