Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- new Client
- {
- ClientId = "mvc",
- ClientName = "MVC Client",
- //AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
- AllowedGrantTypes = GrantTypes.ClientCredentials,
- //RequireConsent = true,
- ClientSecrets =
- {
- new Secret("secret".Sha256())
- },
- //RedirectUris = { "http://localhost:5002/signin-oidc" },
- //PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
- AllowedScopes = { "api2" }
- //AllowedScopes =
- //{
- // IdentityServerConstants.StandardScopes.OpenId,
- // IdentityServerConstants.StandardScopes.Profile,
- // "api2"
- //},
- //AllowOfflineAccess = true
- }
- public void Configuration(IAppBuilder app)
- {
- OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
- //Token Consumption
- app.UseOAuthBearerAuthentication(OAuthBearerOptions);
- // Para obtener más información sobre cómo configurar la aplicación, visite https://go.microsoft.com/fwlink/?LinkID=316888
- app.UseCookieAuthentication(new CookieAuthenticationOptions
- {
- AuthenticationType = "Cookies"
- });
- app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
- {
- AuthenticationType = "oidc",
- SignInAsAuthenticationType = "Cookies",
- Authority = "http://localhost:5000", //ID Server SSO Server
- ClientId = "mvc",
- ClientSecret = "secret",
- ResponseType = "code id_token",
- //RedirectUri = "http://localhost:55392/signin-oidc", //URL of Client website
- //PostLogoutRedirectUri = "http://localhost:55392/signout-callback-oidc", //URL of Client website
- Scope = "api2",
- //AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
- RequireHttpsMetadata = false,
- });
- }
- Console.ReadKey();
- // discover endpoints from metadata
- var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
- if (disco.IsError)
- {
- Console.WriteLine(disco.Error);
- return;
- }
- // request token
- var tokenClient = new TokenClient(disco.TokenEndpoint, "mvc", "secret");
- var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api2");
- if (tokenResponse.IsError)
- {
- Console.WriteLine(tokenResponse.Error);
- return;
- }
- Console.WriteLine(tokenResponse.Json);
- Console.WriteLine("nn");
- // call api
- var client = new HttpClient();
- client.SetBearerToken(tokenResponse.AccessToken);
- var response = await client.GetAsync("http://localhost:55392/api/protected");
- if (!response.IsSuccessStatusCode)
- {
- Console.WriteLine(response.StatusCode);
- }
- else
- {
- var content = await response.Content.ReadAsStringAsync();
- Console.WriteLine(JArray.Parse(content));
- }
- Console.ReadKey();
Add Comment
Please, Sign In to add comment