Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. new Client
  2. {
  3. ClientId = "js",
  4. ClientName = "javascript client",
  5. AllowedGrantTypes = GrantTypes.Implicit,
  6. AllowAccessTokensViaBrowser= true,
  7. RedirectUris = {"http://localhost:5004/callback.html"},
  8. PostLogoutRedirectUris = {"http://localhost:5004/index.html"},
  9. AllowedCorsOrigins = {"http://localhost:5004"},
  10.  
  11. AllowedScopes =
  12. {
  13. StandardScopes.OpenId.Name,
  14. StandardScopes.Profile.Name,
  15. "api1",
  16. "role",
  17. StandardScopes.AllClaims.Name
  18. }
  19. }
  20.  
  21. return new List<Scope>
  22. {
  23. StandardScopes.OpenId,
  24. StandardScopes.Profile,
  25.  
  26. new Scope
  27. {
  28. Name = "api1",
  29. Description = "My API"
  30. },
  31. new Scope
  32. {
  33. Enabled = true,
  34. Name = "role",
  35. DisplayName = "Role(s)",
  36. Description = "roles of user",
  37. Type = ScopeType.Identity,
  38. Claims = new List<ScopeClaim>
  39. {
  40. new ScopeClaim("role",false)
  41. }
  42. },
  43. StandardScopes.AllClaims
  44. };
  45.  
  46. return new List<InMemoryUser>
  47. {
  48. new InMemoryUser
  49. {
  50. Subject = "1",
  51. Username = "alice",
  52. Password = "password",
  53.  
  54. Claims = new List<Claim>
  55. {
  56. new Claim("name", "Alice"),
  57. new Claim("website", "https://alice.com"),
  58. new Claim("role","FreeUser")
  59. }
  60. },
  61. new InMemoryUser
  62. {
  63. Subject = "2",
  64. Username = "bob",
  65. Password = "password",
  66.  
  67. Claims = new List<Claim>
  68. {
  69. new Claim("name", "Bob"),
  70. new Claim("website", "https://bob.com"),
  71. new Claim("role","PaidUser")
  72. }
  73. }
  74. };
  75.  
  76. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  77. {
  78. loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  79. loggerFactory.AddDebug();
  80.  
  81.  
  82. JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
  83. app.UseCors("default");
  84. app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
  85. {
  86. Authority = "http://localhost:5000",
  87. ScopeName = "api1",
  88. // AdditionalScopes = new List<string> { "openid","profile", "role" },
  89. RequireHttpsMetadata = false
  90. });
  91.  
  92. app.UseMvc();
  93. }
  94.  
  95. namespace Api.Controllers
  96. {
  97. [Route("[controller]")]
  98.  
  99. public class IdentityController : ControllerBase
  100. {
  101. [HttpGet]
  102. [Authorize(Roles = "PaidUser")]
  103. public IActionResult Get()
  104. {
  105. return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
  106. }
  107.  
  108. [Authorize(Roles = "FreeUser")]
  109. [HttpGet]
  110. [Route("getfree")]
  111. public IActionResult GetFreeUser()
  112. {
  113. return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
  114. }
  115. }
  116. }
  117.  
  118. var mgr = new Oidc.UserManager(config);
  119. mgr.getUser().then(function (user) {
  120. if (user) {
  121. log("User logged in", user.profile);
  122. } else {
  123. log("User is not logged in.");
  124. }
  125. });
  126.  
  127. function login() {
  128. mgr.signinRedirect();
  129. }
  130.  
  131. function api() {
  132. mgr.getUser().then(function (user) {
  133. var url = "http://localhost:5001/identity/getfree";
  134.  
  135. var xhr = new XMLHttpRequest();
  136. xhr.open("GET", url);
  137. xhr.onload = function () {
  138. log(xhr.status, JSON.parse(xhr.responseText));
  139. };
  140.  
  141. xhr.setRequestHeader("Authorization", "Bearer " + user.access_token);
  142. xhr.send();
  143. });
  144. }
  145.  
  146. function logout() {
  147. mgr.signoutRedirect();
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement