我在我的 asp.net web api 中设置了客户授权属性
全局文件:
FilterConfig.RegisterHttpFilters(GlobalConfiguration.Configuration.Filters);
Run Code Online (Sandbox Code Playgroud)
FilterConfig.cs
public static void RegisterHttpFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
filters.Add(new TokenAuthentication(""));
}
Run Code Online (Sandbox Code Playgroud)
认证类别:
public class TokenAuthentication : Attribute, IAuthenticationFilter
{
private readonly string realm;
public bool AllowMultiple { get { return false; } }
public TokenAuthentication(string realm)
{
this.realm = "realm=" + realm;
}
public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
var request = context.Request;
// Receive token from the client. Here is the example when token is in header:
string token = null;
if …Run Code Online (Sandbox Code Playgroud) 为了了解有关 OAuth 的更多信息,我正在尝试编写一个 OAuth 2.0 提供者和消费者。
我有点使用Doorkeeper Gem作为我的提供商的参考,但我想编写自己的。
我的问题是关于规范第 1.3 节中关于不记名代币的最后一个项目符号。
(F) 资源服务器验证访问令牌,如果有效,则为请求提供服务。
在这种情况下,确实The resource server validates the access token意味着:
我有一个 ASP.NET MVC 项目。在这个项目中,用户可以登录我的网站。所有登录的用户都会进入同一页面的登录页面。我的网站有一个单独的部分,仅针对某些用户进行划分。这是在 Web.config 中通过以下内容定义的:
<location path="SectionedOff">
<system.web>
<authorization>
<allow users="user1,user2,user3" />
<deny users="*" />
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
如果用户已获得此部分的授权,我想在页面上显示一个按钮,每个人在登录时都会重定向到该按钮。这是我想要完成的任务的粗略绘图:
我已经完成了身份验证和适当的授权,我只是不确定如何根据用户是否获得网站单独部分的授权来显示和隐藏此按钮。我隐隐怀疑我必须读取 Web.config 文件才能获取此信息,但是,我不知道如何读取 Web.config 中某个位置的信息。任何帮助,将不胜感激 :)
我正在为我的应用程序设置 2 个可能的策略
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
auth.AddPolicy("apiKey", policy =>
policy.Requirements.Add(new ApiKeyRequirement()));
});
Run Code Online (Sandbox Code Playgroud)
调用我的控制器时必须使用 [Authorize(Policy="Bearer")] 或 [Authorize(Policy="ApiKey")]。
有没有办法将其中一个设置为默认值,然后,当我想使用它时,我可以只使用 [Authorize],如果我想使用另一个,我指定它?
例如:
将“Bearer”设置为默认值,然后,当使用Bearer方案时,我只需要设置[Authorize],但如果我想使用“ApiKey”,则使用[Authorize(Policy="ApiKey")]指定它
找不到任何例子。
我一直在努力使多个身份验证方案在 Asp.net 核心 2.1 中正常工作。
我使用带有隐式流和 OpenIdConnect 作为协议的 Identity Server。
当仅使用其中一种方案(例如 Cookie 或 Bearer)授权操作或控制器时,该功能可以正常工作。
例子:
[Authorize(AuthenticationSchemes = "Cookies")]
[Route("Cookies")]
public class BearerAndCookiesController : Controller {
Run Code Online (Sandbox Code Playgroud)
但是,如果我在 Authorize 属性上指定了两种方案,那么它会部分失败。Bearer 正常工作,但是当我尝试在浏览器中查看页面时,它尝试重定向到本地登录页面 ( http://localhost/Account/Login )。
当我检查 Identity Server 的调试日志时,没有返回任何内容,这是有道理的,因为它没有尝试联系管理局。但是,当我查看测试 MVC 站点的调试日志时,Bearer 和 Cookie 方案都受到挑战:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:5002/cookies
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Route matched with {action = "Get", controller = "BearerAndCookies"}. Executing action MvcClient.Controllers.BearerAndCookiesController.Get (MvcClient)
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes (Bearer, …Run Code Online (Sandbox Code Playgroud) cookies authorization bearer-token asp.net-core identityserver4
我正在开发一个 java spring boot 项目,我正在尝试使用 JWT 为用户身份验证设置 spring 安全性,我正在关注的教程(以及我在互联网上找到的许多教程和项目)讨论了两个部分-认证和授权。
在大多数教程中,有两个过滤器类,一个处理身份验证,另一个处理授权!(有些我发现只有一个类扩展了OncePerRequestFilter类)。
在那些有两个过滤器类的项目中, Authentication 过滤器类扩展了UsernamePasswordAuthenticationFilter类。授权类扩展BasicAuthenticationFilter类。
有没有办法只能在我的项目中使用身份验证部分,或者我应该使用这两个类在 spring 安全中设置用户身份验证?
任何解释将不胜感激。
java authentication authorization spring-security spring-boot
我正在 d3.js v5.8.2 中处理一些图表,我想加载从需要授权标头的 API 请求中获取的 JSON 数据。我试过类似的东西:
d3.json('url')
.header("Authorization", "Bearer 7tsBVpsiYT")
.get(function(error, data) {
// callback
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
未捕获的类型错误:d3.json(...).header 不是函数
假设我正在开发博客平台,用户可以在其中注册帐户、支付订阅费用并创建自己的博客。平台由以下微服务组成:
我正在考虑实现 api-gw 模式,其中除 api-gw 之外的所有微服务都将部署到私有网络(在那里它们将能够通过消息代理同步或异步地直接相互交谈),并且它们只能通过api-gw。
API 将有两个客户端/消费者:
因此我想使用单独的api-gw-per-client模式,所以实际上会有两个api网关,一个用于常规前端(frontent-api-gw),一个用于cms(cms-api-gw),但两者都将使用相同的微服务。
我的问题是关于授权以及它应该在哪里进行(或者更确切地说,不同方法的优缺点是什么)。让我们关注两个“端点”:
前端 api-gw 公开端点以创建新博客,并且此 api 调用“转发”到 blog-service::createBlog() 端点。假设用户已经通过身份验证(即,带有用户 ID 的正确 JWT 与请求一起传递给 api-gw)。
必须进行的授权是确定具有此 ID 的用户是否可以创建新博客。这可以通过调用 subscription-service 检查用户是否已付费订阅来完成。主要问题是该授权是否仍应在 api-gw 端 (A) 或博客服务端 (B) 进行:
类似的情况 - 是否应该将任何格式的 userContext / JWT 传递给每个单独的微服务,并且该微服务应该决定返回什么?或者个别微服务不应该知道 userContext(可能只是为了记录目的),依赖 API GW 授权而只接收一些参数/参数?
我的想法:
在案例 A 中,由于授权层,每个单独的微服务中的逻辑更加复杂。如果有更多 API gw、用户角色等,可能会变得更复杂。 然而,在这种情况下,API GW 更简单,它只将请求转发到微服务。
在情况 B 中,每个单独的微服务中的逻辑不那么复杂、简单和直接。但是API GW中有更多的逻辑,因为它必须为所有平台实现授权(至少对于这个api GW负责的部分)。也许将所有授权集中在一个地方而不是跨微服务传播也是一种优势?
同样,在 B 的情况下,我认为各个微服务之间的耦合较少。
你们如何看待这两种方法/也许您还有其他“想法”?
我已经成功地在我的 WebAPI 中设置了 JWT 身份验证/授权,但是有一个问题:我可以创建一个新的用户帐户,生成它的 JWT 令牌,然后在令牌仍然有效时删除该帐户。 在授权之前,我应该如何以及在哪里检查与令牌关联的用户是否确实存在?
这是我设置 JWT ( Startup.cs) 的代码:
var secretKey = Configuration.GetValue<string>("SecretKey");
var symmetricKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "localhost",
ValidAudience = "localhost",
IssuerSigningKey = symmetricKey
};
});
Run Code Online (Sandbox Code Playgroud)
我在[Authorize]控制器上使用该属性,并且用户 ID 在 JWT 令牌中。
提前致谢!
authentication authorization jwt asp.net-core asp.net-core-webapi
根据 Apple 的说法,如果您在“未确定”授权时要求您的 Core Location 应用程序获得 Always 授权,则用户会看到“When In Use”授权对话框,但实际上您的应用程序获得了 Always 授权 - 临时授权。
这应该意味着如果您实际上不使用您的 Always 权力,您将失去它们,恢复到使用时。
好的,但是这种逆转何时会发生?我似乎无法让它发生。我的应用程序只是停留在 Always 授权,即使用户认为它只是 When In Use 授权。
这是我的测试应用程序(iOS 14)的完整代码:
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var label: UILabel!
let locman = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locman.delegate = self
}
@IBAction func doAskForAlways(_ sender: Any) {
self.checkForLocationAccess(always:true)
}
func checkForLocationAccess(always:Bool = false, andThen f: (()->())? = nil) {
let status = self.locman.authorizationStatus()
switch status {
case .authorizedWhenInUse: …Run Code Online (Sandbox Code Playgroud) authorization ×10
asp.net-core ×2
.net-core ×1
architecture ×1
asp.net ×1
asp.net-mvc ×1
bearer-token ×1
c# ×1
cookies ×1
d3.js ×1
fetch-api ×1
ios ×1
ios14 ×1
java ×1
jwt ×1
oauth-2.0 ×1
policy ×1
spring-boot ×1