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>