Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. public class ServerAppStartup
  2. {
  3. public static void Configuration(IAppBuilder app)
  4. {
  5. app.Map("/identity", idsrvApp =>
  6. {
  7. var factory = new IdentityServerServiceFactory {...};
  8. idsrvApp.UseIdentityServer(new IdentityServerOptions
  9. {
  10. SiteName = "server app",
  11. SigningCertificate = ...,
  12. RequireSsl = false,
  13. Factory = factory,
  14. AuthenticationOptions = new AuthenticationOptions {
  15. RememberLastUsername = true
  16. },
  17. EnableWelcomePage = false
  18. });
  19. });
  20.  
  21. app.SetDefaultSignInAsAuthenticationType("ClientCookie");
  22.  
  23. app.UseCookieAuthentication(new CookieAuthenticationOptions
  24. {
  25. AuthenticationMode = AuthenticationMode.Active,
  26. AuthenticationType = "ClientCookie",
  27. CookieName = CookieAuthenticationDefaults.CookiePrefix + "ClientCookie",
  28. ExpireTimeSpan = TimeSpan.FromMinutes(5)
  29. });
  30.  
  31. app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
  32. {
  33. AuthenticationMode = AuthenticationMode.Active,
  34. AuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType,
  35. SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType(),
  36.  
  37. Authority = options.BaseUrl+ "identity",
  38.  
  39. ClientId = options.ClientId,
  40. RedirectUri = options.RedirectUri,
  41. PostLogoutRedirectUri = options.PostLogoutRedirectUri,
  42.  
  43. ResponseType = "code id_token",
  44. Scope = "openid profile offline_access",
  45.  
  46. Notifications = new OpenIdConnectAuthenticationNotifications
  47. {
  48. AuthorizationCodeReceived = async n =>
  49. {
  50. /* stuff to get ACCESS TOKEN from CODE TOKEN */
  51. },
  52.  
  53. RedirectToIdentityProvider = n =>
  54. {
  55. if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
  56. {
  57. var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token");
  58.  
  59. if (idTokenHint != null)
  60. {
  61. n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
  62. }
  63. }
  64.  
  65. return Task.FromResult(0);
  66. }
  67. }
  68. }
  69.  
  70. JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
  71.  
  72. app.UseNancy();
  73. app.UseStageMarker(PipelineStage.MapHandler);
  74. }
  75.  
  76. using System;
  77. using Nancy.ModelBinding;
  78. using Nancy.Security;
  79. namespace Server.Modules
  80. {
  81. public class UsersModule : BaseModule
  82. {
  83. public UsersModule() : base("/users")
  84. {
  85. Get["/getall"] = parameters =>
  86. {
  87. this.RequiresMSOwinAuthentication();
  88. ...
  89. return ...;
  90. };
  91. }
  92. }
  93. }
  94.  
  95. public class TestServer: IDisposable
  96. {
  97. private Func<IDictionary<string, object>, Task> _appFunc;
  98. public static CookieContainer CookieContainer;
  99. public Uri BaseAddress { get; set; }
  100. // I uses OwinHttpMessageHandler becaouse it can handle http redirections
  101. public OwinHttpMessageHandler Handler { get; private set; }
  102. public HttpClient HttpClient => new HttpClient(Handler) { BaseAddress = BaseAddress };
  103.  
  104. public static TestServer Create()
  105. {
  106. CookieContainer = new CookieContainer();
  107. var result = new TestServer();
  108.  
  109. var appBuilder = new AppBuilder();
  110. appBuilder.Properties["host.AppName"] = "WebApi server";
  111.  
  112. /* Use configuration of server app */
  113. ServerAppStartup.Configuration(appBuilder);
  114.  
  115. result._appFunc = appBuilder.Build();
  116. result.Handler = new OwinHttpMessageHandler(result._appFunc)
  117. {
  118. AllowAutoRedirect = true,
  119. AutoRedirectLimit = 1000,
  120. CookieContainer = CookieContainer,
  121. UseCookies = true
  122. };
  123.  
  124. return result;
  125. }
  126.  
  127. public void Dispose()
  128. {
  129. Handler.Dispose();
  130. GC.SuppressFinalize(this);
  131. }
  132. }
  133.  
  134. namespace ServerSpec.Specs.Users
  135. {
  136. public class GetAllUsersSpec
  137. {
  138. private TestServer _server;
  139.  
  140. public GetAllUsersSpec(){
  141. server = TestServer.create();
  142. }
  143.  
  144. [Fact]
  145. public void should_return_all_users()
  146. {
  147. /* here I will get error because http client or rather its cookie handler has no authentication cookie */
  148. var users = Get("/users/getall");
  149. ...
  150. }
  151.  
  152. public TResponse Get<TResponse>(string urlFragment)
  153. {
  154. var client = server.HttpClient();
  155.  
  156. var httpResponse = client.GetAsync(urlFragment).Result;
  157. httpResponse.EnsureSuccessStatusCode();
  158. return httpResponse.Content.ReadAsAsync<TResponse>().Result;
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement