C# 来自.net framework 4.6的IdentityServer 4
我的问题是关于Identity Server 4以及从预先存在的.net framework MVC应用程序调用它 我已经通过了ID4“快速启动”,以使其能够运行并正确响应示例.net核心MVC应用程序 作为一个快速测试,我创建了一个基本的.Net Framework MVC应用程序,并创建了一个startup.cs文件C# 来自.net framework 4.6的IdentityServer 4,c#,asp.net-mvc,identity,identityserver4,C#,Asp.net Mvc,Identity,Identityserver4,我的问题是关于Identity Server 4以及从预先存在的.net framework MVC应用程序调用它 我已经通过了ID4“快速启动”,以使其能够运行并正确响应示例.net核心MVC应用程序 作为一个快速测试,我创建了一个基本的.Net Framework MVC应用程序,并创建了一个startup.cs文件 using Microsoft.Owin; using Microsoft.Owin.Security.Cookies; using Microsoft.Owin.Securi
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
using System.Collections.Generic;
using System.IdentityModel.Tokens;
[assembly: OwinStartup(typeof(MVC_OWIN_Client.Startup))]
namespace MVC_OWIN_Client
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
string baseClientAddress = "http://localhost:44301/";
var authority = JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = "mvc.standard",
Authority = "http://localhost:5000/",
RedirectUri = baseClientAddress + "signin-oidc",
PostLogoutRedirectUri = baseClientAddress + "signout-callback-oidc",
ResponseType = "code id_token",
Scope = "openid api1 offline_access",
UseTokenLifetime = false,
SignInAsAuthenticationType = "Cookies"
});
}
}
}
就这样。MVC应用程序永远不会恢复,它会一直保存到超时
有没有人有过Identity Server的使用经验,并且能够告诉我这其中是否缺少一些东西?Andy,提前感谢您的时间。如果您在这方面遇到困难,并且无法找到明确的指导,那么答案是根本不要使用UseOpenIdConnectAuthentication。在.net framework mvc应用程序中使用WSFederationAuthentication Startup.cs:-
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.WsFederation;
using Owin;
[assembly: OwinStartup(typeof(MvcOwinWsFederation.Startup))]
namespace MvcOwinWsFederation
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
{
MetadataAddress = "http://localhost:5000/wsfederation",
Wtrealm = "urn:owinrp",
SignInAsAuthenticationType = "Cookies"
});
}
}
}
将客户端设置为在IdentityServer4中进行测试
public static IEnumerable<Client> GetClients()
{
return new[]
{
new Client
{
ClientId = "urn:owinrp",
ProtocolType = ProtocolTypes.WsFederation,
RedirectUris = { "http://localhost:10313/" },
FrontChannelLogoutUri = "http://localhost:10313/home/signoutcleanup",
IdentityTokenLifetime = 36000,
AllowedScopes = { "openid", "profile" }
}
}
}
应该都能用。在这里找到这个
Andy。使用以下设置将integrated identity server 4 open Id连接到asp.net MVC.net Framework cookie base身份验证 确保您已经安装了以下nuget Microsoft.Owin.Security Microsoft.Owin.Security.Cookies Microsoft.Owin.Security.OpenIdConnect 注意:使用应用程序localhost端口更新端口号
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions {});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "oidc",
SignInAsAuthenticationType = "Cookies",
Authority = "http://localhost:5000/",
RedirectUri = "http://localhost:44301/signin-oidc",
PostLogoutRedirectUri = "http://localhost:44301/signout-callback-oidc",
ClientId = "CP",
ResponseType = "id_token",
Scope = "openid profile",
UseTokenLifetime = false,
RequireHttpsMetadata = false,
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = (context) =>
{
var identity = context.AuthenticationTicket.Identity;
var name = identity.Claims.FirstOrDefault(c => c.Type == identity.NameClaimType)?.Value;
return Task.FromResult(0);
}
}
});
}
我使用.NET framework 4.6.1创建Web API,对于Identity Provider,我使用的是Identity Server 4(之前我使用的是Identity Server 3)。起初我确实遇到了一个问题,但我想出了一个对我来说非常有效的解决办法请注意,我正在使用ClientCredentials grantflow。
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["AuthServerURI"],
RequiredScopes = new[] { "comp-read", "stats-read" },
ClientId = "apiname",
ClientSecret = "apisecret",
ValidationMode = ValidationMode.ValidationEndpoint,
EnableValidationResultCache = true
});
我使用相同的IdentityServer3.AccessTokenValidation库进行令牌验证诀窍是瞄准内省的终点。下面是我的配置,它起到了作用
这是Web API的配置,请注意,在这里设置ValidationMode属性非常重要。
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["AuthServerURI"],
RequiredScopes = new[] { "comp-read", "stats-read" },
ClientId = "apiname",
ClientSecret = "apisecret",
ValidationMode = ValidationMode.ValidationEndpoint,
EnableValidationResultCache = true
});
这就是在我的IDP中配置APIResource和Client的方式
new ApiResource
{
Name = "apiname",
DisplayName = "Web API",
Description = "Access to Web API Services",
ApiSecrets = new List<Secret> {new Secret("apisecret".Sha256())},
Scopes = new List<Scope> {
new Scope("comp-read", "Read access to Comp Db API"),
new Scope("stats-read", "Read access to Stats API")
}
新资源
{
Name=“apiname”,
DisplayName=“Web API”,
Description=“访问Web API服务”,
ApiSecrets=new List{new Secret(“apisecret.Sha256())},
范围=新列表{
新范围(“comp read”,“comp Db API的读取访问”),
新范围(“stats read”,“stats API的读取访问”)
}
这是一个客户端的配置方式
new Client
{
ClientName = "name of the client",
ClientId = "clientname",
Enabled = true,
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets = new List<Secret> {
new Secret("secretforclient".Sha256())
},
AllowedScopes = new List<string>
{
"stats-read"
},
AccessTokenLifetime = 1000
}
新客户端
{
ClientName=“客户端的名称”,
ClientId=“clientname”,
启用=真,
AllowedGrantTypes=GrantTypes.ClientCredentials,
ClientSecrets=新列表{
新秘密(“secretforclient.Sha256())
},
AllowedScopes=新列表
{
“数据读取”
},
AccessTokenLifetime=1000
}
希望这能有所帮助。谢谢。下面是一个更新的(2020年)示例,介绍如何在NET4.6中实现IdentityServer4,包括PKCE
博客:Github: PKCE位太长,无法复制,但基本配置如下。注意,URL配置中有否
登录oidc
/注销回调oidc
后缀
公共类启动
{
公共无效配置(IAppBuilder应用程序)
{
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=“cookie”
});
app.UseOpenIdConnectAuthentication(新的OpenIdConnectAuthenticationOptions
{
ClientId=“mvc.owin”,
权威=”http://localhost:5000",
重定向URI=”http://localhost:5001/",
ResponseType=“代码id\u令牌”,
Scope=“openid profile api1”,
SignInAsAuthenticationType=“cookie”,
UseTokenLifetime=false,
代码=true,
SaveTokens=true,
ClientSecret=“secret”
});
}
}
您能详细解释一下“它会一直放在那里直到超时”的意思吗?您的代码在登录和权限请求页面后看起来很好,浏览器变为空白,地址栏中有以下内容。“…&x-client-SKU=ID\u NET&x-client-ver=1.0.40306.1554”在浏览器超时之前,它一直保持空白。这确实很奇怪。你应该在Identity Server github中发布一个bug,在地址栏中超时后,他们应该能够快速发现问题,并在浏览器中重置错误连接。我已经读到MS Owin中有一个bug(Katana bug 197),所以我添加了一个对app.usekentorovicookiesaver()的调用,但这也没有效果。Lol…Open Id connect是一种协议,它受所有服务器端技术的支持,您可以在asp.net core提供的一些技术中使用它,或者我们需要实现相同的技术。如果asp.net.net framework中没有现成的解决方案,那么我们需要实现它。您的“解决方案”不需要“不起作用,但您否决了一个解决方案,因为它使用ws-federation,并且是由identity server的一位作者从一个示例中选取的。提到解决方案在我的应用程序中正常工作,我认为您遗漏了一些内容,请共享我们的代码以便进一步调查。您使用什么作为授权属性?我想强制执行po。”控制器上的策略,我不知道如何执行…:(您好,您可以使用以下事件并生成authenticate cookie或会话来管理应用程序级别的用户策略SecurityTokenValidated=(上下文)=>{var identity=context.AuthenticationTicket.identity;var name=identity.Claims.FirstOrDefault(c=>c.Type==identity.NameClaimType)?.Value;//TODO:gen
new ApiResource
{
Name = "apiname",
DisplayName = "Web API",
Description = "Access to Web API Services",
ApiSecrets = new List<Secret> {new Secret("apisecret".Sha256())},
Scopes = new List<Scope> {
new Scope("comp-read", "Read access to Comp Db API"),
new Scope("stats-read", "Read access to Stats API")
}
new Client
{
ClientName = "name of the client",
ClientId = "clientname",
Enabled = true,
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets = new List<Secret> {
new Secret("secretforclient".Sha256())
},
AllowedScopes = new List<string>
{
"stats-read"
},
AccessTokenLifetime = 1000
}