Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------
- Startup.cs:
- ---------------------------------------------------------
- services.AddIdentityServer()
- .AddJwtBearerClientAuthentication()
- .AddDeveloperSigningCredential()
- .AddInMemoryPersistedGrants()
- .AddInMemoryIdentityResources(Config.GetIdentityResources())
- .AddInMemoryApiResources(Config.GetApiResources())
- .AddInMemoryClients(Config.GetClients())
- .AddAspNetIdentity<ApplicationUser>();
- services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddIdentityServerAuthentication(o =>
- {
- o.Authority = "http://localhost:5000";
- o.RequireHttpsMetadata = false;
- o.ApiName = "api2";
- });
- ---------------------------------------------------------
- HomeController.cs
- ---------------------------------------------------------
- [Authorize(AuthenticationSchemes = "Bearer")]
- public IActionResult Secured()
- {
- return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
- }
- ---------------------------------------------------------
- Wywołanie klienta z innej aplikacji MVC:
- ---------------------------------------------------------
- public async Task<IActionResult> ClientSecured()
- {
- var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
- if (disco.IsError)
- {
- return Content(disco.Error);
- }
- var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret");
- var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api2");
- if (tokenResponse.IsError)
- {
- return Content(tokenResponse.Error);
- }
- HttpClient client = new HttpClient();
- client.SetBearerToken(tokenResponse.AccessToken);
- var response = await client.GetAsync("http://localhost:5000/home/secured");
- if (!response.IsSuccessStatusCode)
- {
- return Content("!response.IsSuccessStatusCode");
- }
- var content = await response.Content.ReadAsStringAsync();
- return Content(content);
- }
Advertisement
Add Comment
Please, Sign In to add comment