Guest User

Untitled

a guest
Jan 19th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. var builder = services
  2. .AddIdentityServer(options =>
  3. {
  4. options.IssuerUri = "https://my.services.is4host";
  5. options.Events.RaiseErrorEvents = true;
  6. options.Events.RaiseInformationEvents = true;
  7. options.Events.RaiseFailureEvents = true;
  8. options.Events.RaiseSuccessEvents = true;
  9. });
  10.  
  11. var migrationAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
  12.  
  13. var inMemorySqlite = new SqliteConnection("Data Source=:memory:");
  14. inMemorySqlite.Open();
  15.  
  16. services
  17. .AddDbContext<SecurityDbContext>(
  18. options => options.UseSqlite(inMemorySqlite,
  19. sql => sql.MigrationsAssembly(migrationAssembly)));
  20.  
  21. // Migrate the database
  22. var context = services.BuildServiceProvider().GetService<SecurityDbContext>();
  23. context.Database.Migrate();
  24.  
  25. builder
  26. .AddInMemoryClients(IS4Config.GetClients())
  27. .AddInMemoryIdentityResources(IS4Config.GetIdentityResources())
  28. .AddInMemoryApiResources(IS4Config.GetAPIResources())
  29. .AddTestUsers(new List<TestUser>
  30. {
  31. new TestUser {
  32. SubjectId = Guid.NewGuid().ToString(),
  33. Claims = new List<Claim> {
  34. new Claim(JwtClaimTypes.Subject, MyDemoUser.ElevatedUser.UserName),
  35. new Claim(JwtClaimTypes.Email, MyDemoUser.ElevatedUser.Email),
  36. new Claim(JwtClaimTypes.Role, MyDemoUser.ElevatedUser.Roles[0]),
  37. new Claim(JwtClaimTypes.Role, MyDemoUser.ElevatedUser.Roles[1])
  38. },
  39. IsActive = true,
  40. Password = MyDemoUser.ElevatedUser.Password,
  41. Username = MyDemoUser.ElevatedUser.UserName
  42. },
  43. new TestUser {
  44. SubjectId = Guid.NewGuid().ToString(),
  45. Claims = new List<Claim> {
  46. new Claim(JwtClaimTypes.Subject, MyDemoUser.WorkstationUser.UserName),
  47. new Claim(JwtClaimTypes.Email, MyDemoUser.WorkstationUser.Email),
  48. new Claim(JwtClaimTypes.Role, MyDemoUser.WorkstationUser.Roles[0])
  49. },
  50. IsActive = true,
  51. Password = MyDemoUser.WorkstationUser.Password,
  52. Username = MyDemoUser.WorkstationUser.UserName
  53. }
  54. });
  55.  
  56. var is4Host = new WebApplicationTestFactory<Services.IS4Host.Startup>();
  57. var client = is4Host.CreateClient();
  58.  
  59. var disco = await client.GetDiscoveryDocumentAsync("https://My.services.is4host");
  60. if (disco.IsError)
  61. {
  62. Console.WriteLine(disco.Error);
  63. return false;
  64. }
  65.  
  66. // request token
  67. var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
  68. {
  69. Address = disco.TokenEndpoint,
  70. ClientId = MyClient.ClientId,
  71. ClientSecret = MyClient.ClientSecret,
  72. Scope = MyApiResource.Gateway.Name,
  73. UserName = user.UserName,
  74. Password = user.Password
  75. });
  76.  
  77. if (tokenResponse.IsError)
  78. {
  79. Console.WriteLine(tokenResponse.Error);
  80. return false;
  81. }
  82.  
  83. Console.WriteLine(tokenResponse.Json);
  84. Console.WriteLine("nn");
  85.  
  86. return true;
  87.  
  88. {
  89. "error": "invalid_grant",
  90. "error_description": "invalid_username_or_password"
  91. }
Add Comment
Please, Sign In to add comment