Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ConfigureServices(IServiceCollection services) {
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- services.AddMvcCore().AddAuthorization().AddJsonFormatters();
- services.AddAuthentication("Bearer")
- .AddIdentityServerAuthentication(options => {
- options.Authority = "http://localhost:5000"; //Identity Server URL
- options.RequireHttpsMetadata = false; // make it false since we are not using https
- options.ApiName = "Api_Name"; //api name which should be registered in IdentityServer
- });
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
- if (env.IsDevelopment()) {
- app.UseDeveloperExceptionPage();
- }
- else {
- app.UseHsts();
- }
- app.UseAuthentication();
- app.UseHttpsRedirection();
- app.UseMvc();
- }
- var identityServer = await DiscoveryClient.GetAsync("http://localhost:5000"); //discover the IdentityServer
- if (identityServer.IsError) {
- Console.Write(identityServer.Error);
- return;
- }
- HttpClient client = new HttpClient();
- var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest {
- Address = identityServer.TokenEndpoint,
- ClientId = "Api_Client",
- ClientSecret = "secret",
- UserName = "Majd",
- Password = "P@ssw0rd@123"
- });
- if (tokenResponse.IsError) {
- Console.WriteLine(tokenResponse.Error);
- return;
- }
- //Call the API
- client.SetBearerToken(tokenResponse.AccessToken);
- var response = await client.GetAsync("https://localhost:44368/api/values");
- var response2 = await client.GetAsync("https://localhost:44368/api/values/1");
- var content = await response.Content.ReadAsStringAsync();
- Console.WriteLine(JArray.Parse(content));
- Console.ReadKey();
Add Comment
Please, Sign In to add comment