Guest User

Untitled

a guest
Oct 17th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. public static IEnumerable<ApiResource> GetApiResources(IConfiguration configuration)
  2. {
  3. return new []
  4. {
  5. new ApiResource
  6. {
  7. Name = "invoices.api",
  8.  
  9. ApiSecrets =
  10. {
  11. new Secret("invoices.api.secret".Sha256()),
  12. },
  13.  
  14. Scopes =
  15. {
  16. new Scope("invoices.api.scope"),
  17. },
  18.  
  19. UserClaims =
  20. {
  21. "custom_role",
  22. }
  23. }
  24. };
  25. }
  26.  
  27. public static IEnumerable<Client> GetClients(IConfiguration configuration)
  28. {
  29. return new []
  30. {
  31. new Client
  32. {
  33. ClientId = "invoices.ui",
  34. RequireConsent = false,
  35. AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
  36. AccessTokenType = AccessTokenType.Reference,
  37.  
  38. AllowedCorsOrigins = configuration.GetSection("Redirect").Get<RedirectOptions>().AllowedCorsOrigins.ToList(),
  39. RedirectUris = configuration.GetSection("Redirect").Get<RedirectOptions>().RedirectUris.ToList(),
  40. PostLogoutRedirectUris = configuration.GetSection("Redirect").Get<RedirectOptions>().PostLogoutRedirectUris.ToList(),
  41.  
  42. ClientSecrets =
  43. {
  44. new Secret("invoices.ui.secret".Sha256())
  45. },
  46.  
  47. AllowedScopes =
  48. {
  49. IdentityServerConstants.StandardScopes.OpenId,
  50. "invoices.api.scope",
  51. },
  52. }
  53. };
  54. }
  55.  
  56. public static IEnumerable<TestUser> GetUsers(IConfiguration configuration)
  57. {
  58. return new []
  59. {
  60. new TestUser
  61. {
  62. SubjectId = "1",
  63. Username = "alice",
  64. Password = "123",
  65. Claims =
  66. {
  67. new Claim("custom_role", "user"),
  68. },
  69. },
  70. new TestUser
  71. {
  72. SubjectId = "2",
  73. Username = "bob",
  74. Password = "123",
  75. Claims =
  76. {
  77. new Claim("custom_role", "admin"),
  78. },
  79. }
  80. };
  81. }
  82.  
  83. public static IEnumerable<IdentityResource> GetIdentityResources(IConfiguration configuration)
  84. {
  85. return new []
  86. {
  87. new IdentityResources.OpenId(),
  88. };
  89. }
  90.  
  91. services.AddAuthentication(options =>
  92. {
  93. options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  94. options.DefaultChallengeScheme = "oidc";
  95. })
  96. .AddCookie(opts =>
  97. {
  98. //opts.ExpireTimeSpan = TimeSpan.FromSeconds(60);
  99. })
  100. .AddOpenIdConnect("oidc", opts =>
  101. {
  102. opts.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  103.  
  104. opts.DisableTelemetry = true;
  105.  
  106. opts.Authority = Configuration.GetValue<string>("IdentityServer");
  107. opts.RequireHttpsMetadata = false;
  108.  
  109. opts.ClientId = "invoices.ui";
  110. opts.ClientSecret = "invoices.ui.secret";
  111. opts.ResponseType = "code id_token";
  112.  
  113. opts.SaveTokens = true;
  114. opts.GetClaimsFromUserInfoEndpoint = true;
  115.  
  116. opts.Scope.Clear();
  117. opts.Scope.Add("openid");
  118. opts.Scope.Add("invoices.api.scope");
  119. });
  120.  
  121. @foreach (var claim in User.Claims)
  122. {
  123. <dt>@claim.Type</dt>
  124. <dd>@claim.Value</dd>
  125. }
Add Comment
Please, Sign In to add comment