Guest User

Untitled

a guest
Dec 9th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. // Copyright (c) Brock Allen, Dominick Baier, Michele Leroux Bustamante. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
  3.  
  4. using Host.AspNetCorePolicy;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Mvc.Authorization;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11.  
  12. namespace Host
  13. {
  14. public class Startup
  15. {
  16. public IConfiguration Configuration { get; }
  17.  
  18. public Startup(IConfiguration configuration)
  19. {
  20. Configuration = configuration;
  21. }
  22.  
  23. public void ConfigureServices(IServiceCollection services)
  24. {
  25. services.AddMvc(options =>
  26. {
  27. // this sets up a default authorization policy for the application
  28. // in this case, authenticated users are required (besides controllers/actions that have [AllowAnonymous]
  29. var policy = new AuthorizationPolicyBuilder()
  30. .RequireAuthenticatedUser()
  31. .Build();
  32. options.Filters.Add(new AuthorizeFilter(policy));
  33. });
  34.  
  35. // this sets up authentication - for this demo we simply use a local cookie
  36. // typically authentication would be done using an external provider
  37. services.AddAuthentication("Cookies")
  38. .AddCookie("Cookies");
  39.  
  40. // this sets up the PolicyServer client library and policy provider - configuration is loaded from appsettings.json
  41. services.AddPolicyServerClient(Configuration.GetSection("Policy"))
  42. .AddAuthorizationPermissionPolicies();
  43.  
  44. // this adds the necessary handler for our custom medication requirement
  45. services.AddTransient<IAuthorizationHandler, MedicationRequirementHandler>();
  46. }
  47.  
  48. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  49. {
  50. app.UseDeveloperExceptionPage();
  51. app.UseAuthentication();
  52.  
  53. // add this middleware to make roles and permissions available as claims
  54. // this is mainly useful for using the classic [Authorize(Roles="foo")] and IsInRole functionality
  55. // this is not needed if you use the client library directly or the new policy-based authorization framework in ASP.NET Core
  56. app.UsePolicyServerClaims();
  57.  
  58. app.UseStaticFiles();
  59. app.UseMvcWithDefaultRoute();
  60. }
  61. }
  62. }
Add Comment
Please, Sign In to add comment