Guest User

Untitled

a guest
May 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. new Client
  2. {
  3. ClientId = "mvc",
  4. ClientName = "MVC Client",
  5. //AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
  6. AllowedGrantTypes = GrantTypes.ClientCredentials,
  7. //RequireConsent = true,
  8.  
  9. ClientSecrets =
  10. {
  11. new Secret("secret".Sha256())
  12. },
  13.  
  14. //RedirectUris = { "http://localhost:5002/signin-oidc" },
  15. //PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
  16. AllowedScopes = { "api2" }
  17.  
  18. //AllowedScopes =
  19. //{
  20. // IdentityServerConstants.StandardScopes.OpenId,
  21. // IdentityServerConstants.StandardScopes.Profile,
  22. // "api2"
  23. //},
  24. //AllowOfflineAccess = true
  25. }
  26.  
  27. public void Configuration(IAppBuilder app)
  28. {
  29.  
  30. OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
  31. //Token Consumption
  32. app.UseOAuthBearerAuthentication(OAuthBearerOptions);
  33.  
  34. // Para obtener más información sobre cómo configurar la aplicación, visite https://go.microsoft.com/fwlink/?LinkID=316888
  35.  
  36. app.UseCookieAuthentication(new CookieAuthenticationOptions
  37. {
  38. AuthenticationType = "Cookies"
  39. });
  40.  
  41. app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
  42. {
  43.  
  44. AuthenticationType = "oidc",
  45. SignInAsAuthenticationType = "Cookies",
  46. Authority = "http://localhost:5000", //ID Server SSO Server
  47. ClientId = "mvc",
  48. ClientSecret = "secret",
  49. ResponseType = "code id_token",
  50. //RedirectUri = "http://localhost:55392/signin-oidc", //URL of Client website
  51. //PostLogoutRedirectUri = "http://localhost:55392/signout-callback-oidc", //URL of Client website
  52. Scope = "api2",
  53. //AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
  54.  
  55. RequireHttpsMetadata = false,
  56.  
  57. });
  58.  
  59. }
  60.  
  61. Console.ReadKey();
  62. // discover endpoints from metadata
  63. var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
  64. if (disco.IsError)
  65. {
  66. Console.WriteLine(disco.Error);
  67. return;
  68. }
  69.  
  70. // request token
  71. var tokenClient = new TokenClient(disco.TokenEndpoint, "mvc", "secret");
  72. var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api2");
  73.  
  74. if (tokenResponse.IsError)
  75. {
  76. Console.WriteLine(tokenResponse.Error);
  77. return;
  78. }
  79.  
  80. Console.WriteLine(tokenResponse.Json);
  81. Console.WriteLine("nn");
  82.  
  83. // call api
  84. var client = new HttpClient();
  85. client.SetBearerToken(tokenResponse.AccessToken);
  86.  
  87. var response = await client.GetAsync("http://localhost:55392/api/protected");
  88. if (!response.IsSuccessStatusCode)
  89. {
  90. Console.WriteLine(response.StatusCode);
  91. }
  92. else
  93. {
  94. var content = await response.Content.ReadAsStringAsync();
  95. Console.WriteLine(JArray.Parse(content));
  96. }
  97. Console.ReadKey();
Add Comment
Please, Sign In to add comment