Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. public class Startup
  2. {
  3.  
  4. private List<TestUser> GetTestUsers()
  5. {
  6. return new List<TestUser>
  7. {
  8. new TestUser
  9. {
  10. SubjectId = "1",
  11. Username = "test",
  12. Password = "password"
  13. }
  14. };
  15. }
  16.  
  17. // This method gets called by the runtime. Use this method to add services to the container.
  18. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  19. public void ConfigureServices(IServiceCollection services)
  20. {
  21. services.AddMvc();
  22. services.AddIdentityServer()
  23. .AddDeveloperSigningCredential()
  24. .AddInMemoryIdentityResources(GetIdentityResources())
  25. .AddInMemoryApiResources(GetApiResources())
  26. .AddInMemoryClients(GetClients())
  27. .AddTestUsers(GetTestUsers());
  28.  
  29. services.AddAuthentication()
  30. .AddOpenIdConnect("oidc", "OpenID Connect", options =>
  31. {
  32. options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
  33. options.SignOutScheme = IdentityServerConstants.SignoutScheme;
  34. options.Authority = "http:localhost:44388";
  35. //??? this should match the client id, right?
  36. options.ClientId = "web-badders-rank";
  37. options.RequireHttpsMetadata = false; // for dev testing
  38. });
  39. }
  40.  
  41. private IEnumerable<Client> GetClients()
  42. {
  43. return new List<Client>
  44. {
  45. new Client
  46. {
  47. ClientId = "web-badders-rank",
  48. ClientName = "Badders Rank Web Client",
  49. AllowedGrantTypes = GrantTypes.Implicit,
  50. AllowAccessTokensViaBrowser = true,
  51. RedirectUris = {"http://localhost:4002/signin-oidc"},
  52. AllowedCorsOrigins = {"http://localhost:4200"},
  53. AllowedScopes =
  54. {
  55. IdentityServerConstants.StandardScopes.OpenId,
  56. IdentityServerConstants.StandardScopes.Profile,
  57. "badders-rank-api"
  58. }
  59. }
  60. };
  61. }
  62.  
  63. private IEnumerable<ApiResource> GetApiResources()
  64. {
  65. return new List<ApiResource>
  66. {
  67. new ApiResource("badders-rank-api", "Badders Rank Api")
  68. };
  69. }
  70.  
  71. private IEnumerable<IdentityResource> GetIdentityResources()
  72. {
  73. return new List<IdentityResource>
  74. {
  75. new IdentityResources.OpenId(),
  76. new IdentityResources.Profile()
  77. };
  78. }
  79.  
  80. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  81. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  82. {
  83. if (env.IsDevelopment())
  84. {
  85. app.UseDeveloperExceptionPage();
  86. }
  87. app.UseIdentityServer();
  88. app.UseStaticFiles();
  89. AccountOptions.ShowLogoutPrompt = false;
  90. AccountOptions.AutomaticRedirectAfterSignOut = true;
  91. app.UseMvcWithDefaultRoute();
  92. }
  93. }
  94.  
  95. public class Startup
  96. {
  97. public Startup(IConfiguration configuration)
  98. {
  99. Configuration = configuration;
  100. }
  101.  
  102. public IConfiguration Configuration { get; }
  103.  
  104. // This method gets called by the runtime. Use this method to add services to the container.
  105. public void ConfigureServices(IServiceCollection services)
  106. {
  107. services.AddMvcCore()
  108. .AddJsonFormatters()
  109. .AddAuthorization();
  110.  
  111. services.AddAuthentication("Bearer")
  112. .AddIdentityServerAuthentication(options =>
  113. {
  114. options.Authority = "http://localhost:44388";
  115. options.ApiName = "badders-rank-api";
  116. options.RequireHttpsMetadata = false;
  117. //options.RequireScope
  118. //options.TokenValidationParameters.NameClaimType = "name";
  119. });
  120.  
  121.  
  122. services.AddCors(option => option.AddPolicy("MyPolicy", builder =>
  123. {
  124. builder.WithOrigins("http://localhost:4200", "https://localhost:44388")
  125. .AllowAnyMethod()
  126. .AllowAnyHeader();
  127. }));
  128.  
  129. services.AddScoped(typeof(IPlayerRepository), typeof(PlayerRepository));
  130. services.AddScoped(typeof(IGroupRepository), typeof(GroupRepository));
  131. }
  132.  
  133. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  134. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  135. {
  136. if (env.IsDevelopment())
  137. {
  138. app.UseDeveloperExceptionPage();
  139. }
  140. app.UseCors("MyPolicy");
  141.  
  142. app.UseAuthentication();
  143.  
  144. app.UseMvc();
  145. }
  146. }
  147.  
  148. import {AuthConfig } from 'angular-oauth2-oidc';
  149.  
  150. export const authConfig: AuthConfig = {
  151. issuer: 'http://localhost:44388',
  152. redirectUri: 'http://localhost:4200/signin-oidc',
  153. clientId: 'web-badders-rank',
  154. responseType : 'id_token token',
  155. scope: 'openid profile badders-rank-api'
  156. };
  157.  
  158. export class AppComponent implements OnInit {
  159. title = 'Badders Rank';
  160. constructor(private oauthService: OAuthService) {
  161. // this.configureWithNewConfigApi();
  162. // console.log(this.oauthService.getAccessToken());
  163. }
  164. private configureWithNewConfigApi(): void {
  165. this.oauthService.configure(authConfig);
  166. this.oauthService.tokenValidationHandler = new JwksValidationHandler();
  167. this.oauthService.loadDiscoveryDocumentAndTryLogin();
  168. }
  169. ngOnInit(): void {
  170. // this.service.getGroupNames().then(res => {
  171. // this.groupNames = res;
  172. // });
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement