带有身份服务器 4 的 asp.net Web 表单客户端 [英] asp.net web form client with identity server 4

查看:16
本文介绍了带有身份服务器 4 的 asp.net Web 表单客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含

的asp.net解决方案

1).asp.net 身份服务器 rc 32).asp.net 核心 web api3).asp.net 网络表单(不在 asp.net 核心、客户端中)

我没有看到任何带有身份服务器 4 和 Web 表单客户端的示例.您能否建议如何使用具有 asp.net 身份的身份服务器对 Web 表单用户进行身份验证,然后使用访问令牌调用 api?

我没有看到带有 网络表单客户端的身份服务器 4 示例sample

身份服务器 3 有一个 sample 但它正在startup 中做所有事情>

当我看到 mvc 客户端 对于身份服务器 4,它在配置方法中有所有设置,然后像 这个

我将如何在 webform 中应用 Authorize 属性,以便我重定向到身份服务器 4 进行登录,然后在登录后,当我像这样调用 api 时:

如何更改 webform 的客户端?

 new Client(){ClientId = "mvcClient",ClientName = "MVC 客户端",AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,ClientSecrets = new List(){新的秘密(秘密".Sha256())},RequireConsent = false;//登录后重定向到哪里RedirectUris = { "http://localhost:5002/signin-oidc" },//注销后重定向到哪里PostLogoutRedirectUris = { "http://localhost:5002" },允许范围 ={StandardScopes.OpenId.Name,StandardScopes.Profile.Name,StandardScopes.OfflineAccess.Name,StandardScopes.Roles.Name,API"}}new InMemoryUser(){主题 = "1",用户名 = "testuser",密码 = "密码",声明 = 新列表<声明>(){新索赔(姓名",爱丽丝"),new Claim("网站", "http://alice.com"),新索赔(JwtClaimTypes.Role,管理员")}}返回新列表<范围>(){StandardScopes.OpenId,//主题 IDStandardScopes.Profile,//名字,姓氏StandardScopes.OfflineAccess,标准范围.角色,新范围(){名称 = "API",描述 = "API 描述",类型 = ScopeType.Resource,强调=真,IncludeAllClaimsForUser = true,声明 = 新列表{新范围声明(声明类型.名称),新范围声明(声明类型.角色)}}};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 结果 = JArray.Parse(content).ToString();}[授权(角色=管理员")][HttpGet]公共 IActionResult Get(){return new JsonResult(from c in User.Claims select new { c.Type, c.Value });}

解决方案

迟到的答案,但希望它对仍然支持 Web 表单的人有所帮助.
与web表单一起使用启动没有问题.唯一的限制是没有 AuthorizeAttribute 的地方,但它仍然不是问题,只需:

app.UseStageMarker(PipelineStage.Authenticate);

在你的底部

public void Configuration(IAppBuilder app)

OWIN 启动中的方法.

启动实现示例 可能是从我的 github 获取.它适用于 MVC、Web 表单,并额外从 IdentityServer v.3 代码库中引入 JWT 验证,升级后可使用最新的 OWIN 库进行编译.


如果我还有什么不清楚的,请不要犹豫,在评论中提问.

I have a asp.net solution which consists of

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

I don't see any sample with identity server 4 and web form client. Can you please suggest how to authenticate web form user using identity server with asp.net identity and then call api with the access token ?

I don't see identity server 4 sample with web form client or sample

identity server 3 has a sample but it is doing everything in startup

When i see mvc client for identity server 4, it has all settings in configure method and then calls it like this

How will i apply Authorize attribute in webform so that i am redirected to identity server 4 for login and then after login when i call api like this:

how to change client for 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 });
                }

解决方案

Late answer, but hopefully it helps someone, still supporting web forms.
There is no problem to use startup together with web forms. The only limitation is no place for AuthorizeAttribute there, but it's still not a problem, just put:

app.UseStageMarker(PipelineStage.Authenticate);

at the bottom of your

public void Configuration(IAppBuilder app)

method within OWIN Startup.

An example Startup implementation could be fetched from my github. It works with MVC, Web Forms and additionally brings JWT validation from IdentityServer v.3' codebase, upgraded to compile with the latest OWIN libraries.


If I still left anything unclear, don't hesitate to ask in the comments.

这篇关于带有身份服务器 4 的 asp.net Web 表单客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆