Guest User

Untitled

a guest
Jan 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. public class Startup
  2. {
  3. // This method gets called by the runtime. Use this method to add services to the container.
  4. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  5. public void ConfigureServices(IServiceCollection services)
  6. {
  7. services.AddMvc();
  8.  
  9. services.AddIdentityServer()
  10. .AddDeveloperSigningCredential()
  11. .AddTestUsers(Config.GetUsers())
  12. .AddInMemoryIdentityResources(Config.GetIdentityResources())
  13. .AddInMemoryClients(Config.GetClients());
  14.  
  15. }
  16.  
  17. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  18. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  19. {
  20. if (env.IsDevelopment())
  21. {
  22. app.UseDeveloperExceptionPage();
  23. }
  24.  
  25. app.UseIdentityServer();
  26.  
  27. app.UseStaticFiles();
  28.  
  29. app.UseMvcWithDefaultRoute();
  30. //app.Run(async (context) =>
  31. //{
  32. // await context.Response.WriteAsync("Hello World!");
  33. //});
  34. }
  35. }
  36.  
  37. public static class Config
  38. {
  39. public static List<TestUser> GetUsers()
  40. {
  41. return new List<TestUser>
  42. {
  43. new TestUser
  44. {
  45. SubjectId ="d866oef",
  46. Username ="Kasunjith",
  47. Password="password",
  48. Claims= new List<Claim>
  49. {
  50. new Claim("given_name","Kasunjith"),
  51. new Claim("family_name","Underwood"),
  52. }
  53. }, new TestUser
  54. {
  55. SubjectId ="d866omf",
  56. Username ="BimalJith",
  57. Password="password",
  58. Claims= new List<Claim>
  59. {
  60. new Claim("given_name","BimalJith"),
  61. new Claim("family_name","ViewWord"),
  62. }
  63. },
  64.  
  65. };
  66.  
  67. }
  68. // identity-related resources (Scopes)
  69. public static IEnumerable<IdentityResource> GetIdentityResources()
  70. {
  71. return new List<IdentityResource>
  72. {
  73. new IdentityResources.OpenId(),
  74. new IdentityResources.Profile()
  75. };
  76. }
  77.  
  78. public static IEnumerable<Client> GetClients()
  79. {
  80. return new List<Client>()
  81. {
  82. new Client
  83. {
  84. ClientName="Image Galary",
  85. ClientId="imagegalleryclient",
  86. AllowedGrantTypes = GrantTypes.Hybrid,
  87. RedirectUris = new List<string>()
  88. {
  89. "https://localhost:44335/signin-oidc"
  90. },
  91. AllowedScopes =
  92. {
  93. IdentityServerConstants.StandardScopes.OpenId
  94. },
  95. ClientSecrets =
  96. {
  97. new Secret("secret".Sha256())
  98. }
  99.  
  100. }
  101. };
  102. }
  103.  
  104. }
  105.  
  106. public class Startup
  107. {
  108. public Startup(IConfiguration configuration)
  109. {
  110. Configuration = configuration;
  111. }
  112.  
  113. public IConfiguration Configuration { get; }
  114.  
  115. // This method gets called by the runtime. Use this method to add services to the container.
  116. public void ConfigureServices(IServiceCollection services)
  117. {
  118. services.AddMvc();
  119.  
  120. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  121.  
  122. services.AddAuthentication(options =>
  123. {
  124. options.DefaultScheme = "Cookies";
  125. options.DefaultChallengeScheme = "oidc";
  126. }).AddCookie("Cookies",
  127. (options) =>
  128. {
  129.  
  130. }).AddOpenIdConnect("oidc", options => {
  131. options.SignInScheme = "Cookies";
  132. options.Authority = "https://localhost:44393";
  133. options.ClientId = "imagegalleryclient";
  134. options.ResponseType = "code id_token";
  135. options.SaveTokens = true;
  136. options.ClientSecret = "secret";
  137.  
  138. });
  139.  
  140.  
  141. }
  142.  
  143. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  144. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  145. {
  146. if (env.IsDevelopment())
  147. {
  148. app.UseBrowserLink();
  149. app.UseDeveloperExceptionPage();
  150. }
  151. else
  152. {
  153. app.UseExceptionHandler("/Home/Error");
  154. }
  155.  
  156. app.UseAuthentication();
  157.  
  158. app.UseStaticFiles();
  159.  
  160. app.UseMvc(routes =>
  161. {
  162. routes.MapRoute(
  163. name: "default",
  164. template: "{controller=Gallery}/{action=Index}/{id?}");
  165. });
  166. }
  167. }
  168.  
  169. [Authorize]
  170. public class GalleryController : Controller
  171. {
  172. public async Task<IActionResult> Index()
  173. {
  174. await WriteOutIdentityInformation();
  175. return View();
  176. }
  177.  
  178.  
  179. public async Task WriteOutIdentityInformation()
  180. {
  181. var identityToken = await HttpContext.GetTokenAsync(OpenIdConnectParameterNames.IdToken);
  182.  
  183.  
  184. Debug.WriteLine($"Identity token:{identityToken}");
  185.  
  186. foreach (var claim in User.Claims)
  187. {
  188. Debug.WriteLine($"Claim type:{ claim.Type} -Claim value : {claim.Value}");
  189. }
  190. }
  191. }
Add Comment
Please, Sign In to add comment