- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
对于提出这个问题,我提前表示歉意,因为我几乎不了解一般的安全性,尤其是 IdentityServer。
我正在尝试设置 IdentityServer 来管理 Asp.Net MVC 应用程序的安全性。
我正在按照他们网站上的教程进行操作:Asp.Net MVC with IdentityServer
但是,我做的事情略有不同,因为我有一个单独的项目用于身份“服务器”部分,这导致 2 个 Startup.cs 文件,一个用于应用程序,一个用于身份服务器
对于应用程序,Startup.cs 文件如下所示
public class Startup
{
public void Configuration(IAppBuilder app)
{
AntiForgeryConfig.UniqueClaimTypeIdentifier = Constants.ClaimTypes.Subject;
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://localhost:44301/identity",
ClientId = "baseballStats",
Scope = "openid profile roles baseballStatsApi",
RedirectUri = "https://localhost:44300/",
ResponseType = "id_token token",
SignInAsAuthenticationType = "Cookies",
UseTokenLifetime = false,
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = async n =>
{
var userInfoClient = new UserInfoClient(
new Uri(n.Options.Authority + "/connect/userinfo"),
n.ProtocolMessage.AccessToken);
var userInfo = await userInfoClient.GetAsync();
// create new identity and set name and role claim type
var nid = new ClaimsIdentity(
n.AuthenticationTicket.Identity.AuthenticationType,
Constants.ClaimTypes.GivenName,
Constants.ClaimTypes.Role);
userInfo.Claims.ToList().ForEach(c => nid.AddClaim(new Claim(c.Item1, c.Item2)));
// keep the id_token for logout
nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
// add access token for sample API
nid.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
// keep track of access token expiration
nid.AddClaim(new Claim("expires_at", DateTimeOffset.Now.AddSeconds(int.Parse(n.ProtocolMessage.ExpiresIn)).ToString()));
// add some other app specific claim
nid.AddClaim(new Claim("app_specific", "some data"));
n.AuthenticationTicket = new AuthenticationTicket(
nid,
n.AuthenticationTicket.Properties);
}
}
});
app.UseResourceAuthorization(new AuthorizationManager());
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "https://localhost:44301/identity",
RequiredScopes = new[] { "baseballStatsApi"}
});
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
}
}
对于身份服务器来说,startup.cs文件是
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("/identity", idsrvApp =>
{
idsrvApp.UseIdentityServer(new IdentityServerOptions
{
SiteName = "Embedded IdentityServer",
SigningCertificate = LoadCertificate(),
Factory = InMemoryFactory.Create(
users: Users.Get(),
clients: Clients.Get(),
scopes: Scopes.Get())
});
});
}
X509Certificate2 LoadCertificate()
{
return new X509Certificate2(
string.Format(@"{0}\bin\Configuration\idsrv3test.pfx", AppDomain.CurrentDomain.BaseDirectory), "idsrv3test");
}
}
我也在设置授权管理器
public class AuthorizationManager : ResourceAuthorizationManager
{
public override Task<bool> CheckAccessAsync(ResourceAuthorizationContext context)
{
switch (context.Resource.First().Value)
{
case "Players":
return CheckAuthorization(context);
case "About":
return CheckAuthorization(context);
default:
return Nok();
}
}
private Task<bool> CheckAuthorization(ResourceAuthorizationContext context)
{
switch(context.Action.First().Value)
{
case "Read":
return Eval(context.Principal.HasClaim("role", "LevelOneSubscriber"));
default:
return Nok();
}
}
}
例如,如果我定义一个用 ResourceAuthorize 属性修饰的 Controller 方法,就像这样
public class HomeController : Controller
{
[ResourceAuthorize("Read", "About")]
public ActionResult About()
{
return View((User as ClaimsPrincipal).Claims);
}
}
然后,当我第一次尝试访问此方法时,我将被重定向到默认登录页面。
但是我不明白的是,为什么当我使用我为应用程序定义的用户登录时(见下文),
public class Users
{
public static List<InMemoryUser> Get()
{
return new List<InMemoryUser>
{
new InMemoryUser
{
Username = "bob",
Password = "secret",
Subject = "1",
Claims = new[]
{
new Claim(Constants.ClaimTypes.GivenName, "Bob"),
new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
new Claim(Constants.ClaimTypes.Role, "Geek"),
new Claim(Constants.ClaimTypes.Role, "LevelOneSubscriber")
}
}
};
}
}
我收到 403 错误,Bearer error="insufficient_scope"。
任何人都可以解释我做错了什么吗?
任何后续访问操作方法的尝试都将返回相同的错误。在我看来,我定义的用户拥有能够访问此方法的正确声明。但是,当我第一次尝试访问此方法时,声明检查只发生一次。登录后我得到一个 cookie,并且在随后尝试访问该方法时不会进行声明检查。
我有点迷路了,如果能帮我解决这个问题,我将不胜感激。
提前致谢。
编辑:这里是 scoles 和客户端类
public static class Scopes
{
public static IEnumerable<Scope> Get()
{
var scopes = new List<Scope>
{
new Scope
{
Enabled = true,
Name = "roles",
Type = ScopeType.Identity,
Claims = new List<ScopeClaim>
{
new ScopeClaim("role")
}
},
new Scope
{
Enabled = true,
Name = "baseballStatsApi",
Description = "Access to baseball stats API",
Type = ScopeType.Resource,
Claims = new List<ScopeClaim>
{
new ScopeClaim("role")
}
}
};
scopes.AddRange(StandardScopes.All);
return scopes;
}
}
和客户端类
public static class Clients
{
public static IEnumerable<Client> Get()
{
return new[]
{
new Client
{
Enabled = true,
ClientName = "Baseball Stats Emporium",
ClientId = "baseballStats",
Flow = Flows.Implicit,
RedirectUris = new List<string>
{
"https://localhost:44300/"
}
},
new Client
{
Enabled = true,
ClientName = "Baseball Stats API Client",
ClientId = "baseballStats_Api",
ClientSecrets = new List<ClientSecret>
{
new ClientSecret("secret".Sha256())
},
Flow = Flows.ClientCredentials
}
};
}
}
我还创建了一个自定义过滤器属性,用于确定何时进行声明检查。
public class CustomFilterAttribute : ResourceAuthorizeAttribute
{
public CustomFilterAttribute(string action, params string[] resources) : base(action, resources)
{
}
protected override bool CheckAccess(HttpContextBase httpContext, string action, params string[] resources)
{
return base.CheckAccess(httpContext, action, resources);
}
}
断点仅在对 url 的初始请求时命中。在后续请求中,不会命中过滤器属性断点,因此不会进行检查。这让我感到惊讶,因为我认为每次请求 url 时都必须进行检查。
最佳答案
需要在用户登录时请求api需要的scope。Scope = "openid profile roles baseballStatsApi"
Authority = "https://localhost:44301/identity",
ClientId = "baseballStats",
Scope = "openid profile roles baseballStatsApi",
ResponseType = "id_token token",
RedirectUri = "https://localhost:44300/",
SignInAsAuthenticationType = "Cookies",
UseTokenLifetime = false,
关于c# - 使用 Asp.Net MVC 应用程序设置 IdentityServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30770951/
在登录并同意后使用任何标准身份提供者(谷歌、Facebook)时,他们会重定向到我的主要身份服务器,并让它重定向到在其中注册的隐式客户端。我如何使用另一个身份服务器作为外部身份提供者实现相同的行为?
我正在开发一个 SPA Web 应用程序,并且我正在使用 IdentityServer4 代码流来处理授权。所以我有以下组件: Angular 客户端应用程序,https://localhost:50
我正在设置 IdentityServer 的一个新实例作为身份提供者。登录时,我想在我的用户对象上设置一些额外的自定义声明。现在,我正在使用以下代码: [HttpPost] public async
我正在尝试遵循本指南 https://identityserver.github.io/Documentation/docs/advanced/customizingViews.html通过将 ass
当使用 IdentityServer 3 时签名证书(用于签名 jwt token )过期会发生什么? 我不清楚,我找不到任何文档,除了可能会收到它已过期的警告。 (Ref. https://iden
我有一个使用 IdentityServer4 进行 token 验证的 API。 我想使用内存中的 TestServer 对这个 API 进行单元测试。我想在内存中的 TestServer 中托管 I
我正在尝试将 Identity Server 4 与 Ocelot 集成并对 WebApp (asp.net core 3.1) 进行身份验证,然后在请求通过身份验证时访问 api。 为此我创建了一个
如果您有多个客户端和一个处理用户身份验证的中央 IdentityServer 4 实例,您将如何管理各个客户端的用户配置文件? 当前情况: 用户点击“管理个人资料” 用户被重定向到 IdentityS
我正在开发一个带有 angular 的前端应用程序和一个带有 Asp.Net Core 的后端应用程序,其中包含用于身份验证的 IdentityServer4(基于此 github project)。
我有一个包含用户的数据库,我该如何开始使用 IdentityServer 创建我的用户的自定义实现?我看到的所有示例都使用了用值硬编码的 InMemoryUser。 有人可以关注this作为指南? 最
我们公司有一个 SSO 应用程序,我希望用 IdentityServer4 或 3 替换大部分身份验证管道。我要替换的版本有自己的动态客户端注册自定义实现(不符合规范)和一个 UI 来管理它。 有nu
我正在使用 IdentityServerV3 对少数客户端的用户进行身份验证。我在配置 IdentityServer 时遇到问题,特定的 user 必须能够登录到特定的“客户端”。 让我们来看下面的场
目前我们正在使用 JWT token 进行身份验证(有效),但我想使用引用 token 。目前我们的配置是这样的: public static class Config { ///
我在启动 IdentityServer 3 时遇到问题。这是一个非常简单的设置,我打算将其用于开发环境,但我在诊断问题时遇到了问题。这是我的代码: Startup.cs using Owin;
我正在使用来自 IdentityServer 4 实例的 OAuth2。基于this example ,我能够登录并调用服务器托管的 API。 如何在不显示提示用户确认注销的 Web View 的情况
我是 IdentityServer 3 的新手,示例和教程使用的是 InMemory 用户、客户端和范围,但我需要这些来自 DB。所以我做了: 启动.cs public void Configurat
我一直在阅读和观看有关 Identity Server 4 的大量内容,但我仍然对它感到有些困惑,因为它似乎有太多的事件部分。 我现在明白这是一个单独的项目,它负责对用户进行身份验证。我仍然不明白的是
我似乎无法理解为什么我会从 IdentityServer 获得 unauthorized_client。我将 oidc-client 与 Angular 4 ui 和 Web API 的 asp.ne
我在 IdentityServer 中设置了以下客户端: new Client { ClientName = "My web application", Enabled = true,
在本地,我有一组 Web 应用程序都可以通过一次登录访问。 为此,我采用了 Identity Server 4。 一旦应用程序发布在本地服务器上,无法远程访问,我就通过 openssl 生成了一个 S
我是一名优秀的程序员,十分优秀!