12

客戶端我有一個asp.net的解決方案,包括asp.net web表單與身份服務器4

1). asp.net identity server rc 3 
2). asp.net Core web api 
3). asp.net webform (not in asp.net core, client) 

我沒有看到標識服務器4和Web窗體的客戶的任何樣品。您能否請建議如何使用身份服務器以asp.net身份驗證Web窗體用戶,然後使用訪問令牌調用API?

我看不出身份服務器4樣本與web form clientsample

身份服務器3具有sample但在做startup

的一切,當我看到mvc client身份服務器4,它擁有所有設置在配置方法,然後調用它像this

我將如何應用webform中的授權屬性,以便我重定向到身份服務器4的登錄,然後登錄後,當我打電話api是這樣的:

如何更改webform的客戶端?

new Client() 
        { 
        ClientId = "mvcClient", 
        ClientName = "MVC Client",      
        AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, 

        ClientSecrets = new List<Secret>() 
        { 
         new Secret("secret".Sha256()) 
        }, 

        RequireConsent = false; 

        // where to redirect to after login 
        RedirectUris = { "http://localhost:5002/signin-oidc" }, 
        // where to redirect to after logout 
        PostLogoutRedirectUris = { "http://localhost:5002" }, 

        AllowedScopes = 
        { 
         StandardScopes.OpenId.Name, 
         StandardScopes.Profile.Name, 
         StandardScopes.OfflineAccess.Name, 
         StandardScopes.Roles.Name, 
         "API" 
        } 
       } 

new InMemoryUser() 
      { 
       Subject = "1", 
       Username = "testuser", 
       Password = "password", 
       Claims = new List<Claim>() 
       { 
        new Claim("name", "Alice"), 
        new Claim("Website", "http://alice.com"), 
        new Claim(JwtClaimTypes.Role, "admin") 

       } 
      } 


return new List<Scope>() 
       { 
        StandardScopes.OpenId, // subject id 
        StandardScopes.Profile, // first name, last name 
        StandardScopes.OfflineAccess, 
        StandardScopes.Roles, 
        new Scope() 
        { 
         Name = "API", 
         Description = "API desc", 
         Type = ScopeType.Resource, 
         Emphasize = true, 
         IncludeAllClaimsForUser = true, 
         Claims = new List<ScopeClaim> 
         { 
          new ScopeClaim(ClaimTypes.Name),  
          new ScopeClaim(ClaimTypes.Role) 
         } 
        } 
       }; 


public void CallApiUsingClientCredentials() 
       { 
        var tokenClient = new TokenClient("http://localhost:5000/connect/token", "mvc", "secret"); 
        var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1"); 

        var client = new HttpClient(); 
        client.SetBearerToken(tokenResponse.AccessToken); 
        var content = await client.GetStringAsync("http://localhost:5001/identity"); 

        var result = JArray.Parse(content).ToString(); 

       } 

[Authorize(Roles="admin)] 
      [HttpGet] 
      public IActionResult Get() 
        { 
         return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); 
       } 

回答

0

在的WebForms,您可以設置授權,web.config

<configuration> 
    <system.web> 
    <authorization> 
     <allow roles="domainname\Managers" /> 
     <deny users="*" /> 
    </authorization> 
    </system.web> 
</configuration> 

answer on StackOverflow

也看一下web.config例如IdentityServer3

<location path="About"> 
    <system.web> 
     <authorization> 
     <deny users="?" /> 
     </authorization> 
    </system.web> 
    </location>