我正在构建我的网站,我想限制我的网站的一部分(管理部分)从正常的公共显示.
到目前为止,我只能使用MembershipProvider和/或RoleProviders等找到示例.说实话,它看起来似乎太多了我想要的东西.如果您在输入字段中键入正确的密码,所有这一切都是让您进入.
我真的不能避开提供商吗?
asp.net asp.net-mvc asp.net-authorization asp.net-authentication asp.net-mvc-2
我们的应用需要通过手机号码或Google登录.我们计划通过Twitter数字进行手机号码验证.
我理解的注册和认证流程如下:
移动应用程序使用Twitter数字或Google登录进行丰富的身份验证(用户可以使用丰富的身份验证而不是打开Web浏览器选项卡,从而获得更好的用户体验).Twitter Digits/Google Sign In返回身份令牌.
移动应用程序将AuthServer调用到SignIn并显示身份令牌.
身份服务器使用Digits服务或Google Auth服务验证所呈现的身份令牌.
一旦验证,AuthServer将尝试查找用户,如果不存在,它将创建一个.
AuthServer将Access Token返回给移动应用程序.
现在,我正在寻找实施步骤#3-4的选项.
目前,我所做的是修改令牌端点代码,该代码将用户名作为电话号码或电子邮件地址和密码作为Google/Twitter数字ID令牌发送.现在,由于auth服务器需要知道发送的密码实际上是需要通过第三方服务验证的令牌,我通过在TokenHint中发送服务名称"Digits"或"Google"来解决它.
这非常有效,但我想知道什么是最干净的方式来支持我想要实现的目标.
在我的ConfigureServices中,我创建了一个基于策略的授权
services.AddAuthorization(options =>
{
options.AddPolicy("test", policy => policy.Requirements.Add(new TestRequirement()));
});
Run Code Online (Sandbox Code Playgroud)
并按如下所示注册处理程序
services.AddSingleton<IAuthorizationHandler, CustomAuthorizationHandler>();
Run Code Online (Sandbox Code Playgroud)
而不是像这样在所有控制器上应用授权
[Authorize(Policy = "test")]
Run Code Online (Sandbox Code Playgroud)
有没有办法像MVC 5中那样全局应用授权属性?
我试过了
services.AddMvc(config=>config.Filters.Add(new AuthorizeAttribute("test")))
Run Code Online (Sandbox Code Playgroud)
但我认为这里有问题。
authorization asp.net-authorization asp.net-core-mvc asp.net-core asp.net-core-2.0
我正在尝试在asp.net core 2.0应用程序中实现自定义授权策略。
在我的Custom AuthorizationHandler中,我有以下检查:
if (!context.User.Identity.IsAuthenticated)
{
this.logger.LogInformation("Failed to authorize user. User is not authenticated.");
return Task.CompletedTask;
}
// ... More authorization checks
Run Code Online (Sandbox Code Playgroud)
这很有意义,因为未经身份验证的用户无权执行我的控制器操作。
我正在使用JWT承载身份验证,并将其配置如下:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "<issuer>",
ValidAudience = "<audience>",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(signingKey)),
ValidateLifetime = false
};
});
Run Code Online (Sandbox Code Playgroud)
我还在注册我的授权要求和处理程序(请注意,这些注册发生在上面配置了身份验证之后,以及在我致电之前serivces.AddMVC():
services.AddAuthorization(options =>
{
options.AddPolicy(Constants.AuthorizationPolicies.MyCustomPolicy,
policy => policy.Requirements.Add(new MyCustomRequirement()));
});
services.AddScoped<IAuthorizationHandler, MyCustomAuthorizationHandler>();
Run Code Online (Sandbox Code Playgroud)
这是我的问题,看来我的授权策略在对jwt令牌进行质询和验证之前正在执行,结果是,由于未对用户进行身份验证,所以我的授权策略失败了。 …
我在一个MVC项目中工作,该项目包含常规MVC控制器和Web API控制器.最初,我使用自定义用户表实现了表单身份验证.但现在我计划使用新的ASP .NET身份,并从基于cookie的表单身份验证更改为基于声明的身份验证和授权.我已经有一个包含自定义字段的表的数据库.所以我需要自定义ASP .NET身份以便与我的表一起工作任何人都可以指导我如何实现这一目标吗?
编辑:
在回复FKutsche时,这是我的用户表.我只保留了重要的列.
用户表
用户身份
用户名
密码
UserTypeId
用户类型表
UserTypeId
用户类型
列名是自解释的,所以我没有描述它们.用户表在UserType表的UserTypeId列上具有外键.
authentication asp.net-mvc asp.net-authorization asp.net-mvc-4 asp.net-web-api
我正在尝试找出是否有一种简单的方法可以让 ASP.NET Core 记录哪个[Authorize]属性失败。我混合了“角色”和“策略”授权属性,但每当其中一个失败时,日志就会显示:
显然,这是正确的行为,并且它不会让具有不正确权限的人进入,但是如果您有多个属性,则必须去找出哪个属性失败了,这有点痛苦。如果日志只是显示Authorization failed for Policy X,那么就很容易找到失败的原因。
有谁知道目前是否可以通过我不知道的某些选项来实现这一点?
编辑:例如:如果我有[Authorize(Policy = "Policy 1")]并且[Authorize(Policy = "Policy 2")]只有“策略 2”失败。我希望看到一些东西告诉我“政策 2”失败了。
编辑:对于任何仍然遇到这个问题的人,这现已由 Microsoft 实现并且是 .NET 5.0 的一部分,请参阅问题https://github.com/aspnet/AspNetCore/issues/7789
我正在尝试实施一项非常精细的政策。这个想法就像图中一样。
每个实体始终与右侧实体具有一对多关系。一个机构可以有很多课程,每个课程可以有很多科目,每个科目可以有很多教学大纲,等等...
有 3 个角色:Administrator, Contributor,Viewer
如果您在顶级实体之一拥有角色,则该角色将传播到下面的其余实体。例如:如果您是课程管理员,那么您就是科目、教学大纲等的管理员...
如果您是课程大纲之一的贡献者,您将成为本课程大纲的以下课程和视频的贡献者。
我尝试使用以下方法解决它Custom Policies:
添加如下要求:
public class AdministratorRequirement : IAuthorizationRequirement
{
public int UserId { get; private set; }
public EntityType EntityType { get; private set; }
public int EntityId { get; private set; }
public AdministratorRequirement(int userId, EntityType entityType, int entityId)
{
UserId = userId;
EntityType = entityType;
EntityId = entityId;
}
} …Run Code Online (Sandbox Code Playgroud) c# asp.net-authorization bearer-token .net-core asp.net-core
使用 ASP.NET 5,我实现了一个授权策略,我希望将其应用于命名空间中的所有控制器,但不适用于项目中的所有控制器。[Authorize(Policy="MyPolicy")]除了分别向每个控制器添加属性之外,还有其他方法可以做到这一点吗?我想避免另一个开发人员向命名空间添加新控制器但忘记应用策略的风险。
手动执行时看起来像这样:
[ApiController]
[Authorize(Policy="MyPolicy")]
[Route("api/DoStuff")]
public class MyController : ControllerBase
{
[HttpGet("foo")]
public GetFoo()
{
}
}
Run Code Online (Sandbox Code Playgroud) asp.net authorization asp.net-authorization asp.net-core asp.net-core-webapi
我将我的应用程序从 .NET Core 2.2 升级到 .NET Core 3.1。当我尝试使用 PostMan 测试我的端点时,我注意到收到 401 Unauth 错误。当我查看标题时,我发现到期时间无效:
我获取了以下不记名令牌:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiQm9iIiwibmJmIjoiMTYxNzk3Nzg1MSIsImV4cCI6IjE2MjMxNjE4NTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOlsiQmFza2V0YmFsbCIsIlJ1Z2J5IiwiRm9vdGJhbGwiXX0.QRLuXFeopf7QZ1NUzWcctuSfnNXiPgc2UH7NxAuHYvw
Run Code Online (Sandbox Code Playgroud)
我正在生成令牌端点并使用jwt.io对其进行解码,exp 字段为“1623161856”。
我将其转换为 Javascript 中的日期对象,它等于未来 60 天。

所以token肯定没有过期。不确定我在升级到 .NET Core 3.1 时是否遗漏了任何内容,但这里是相关代码:
在Startup.cs我有
public void ConfigureServices(IServiceCollection services)
{
// Initial Setup
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
// Call this in case you need aspnet-user-authtype/aspnet-user-identity
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(Configuration["v1"], new OpenApiInfo { Title = Configuration["Sports"], Version = Configuration["v1] });
});
services.AddDataProtection();
//Authentication …Run Code Online (Sandbox Code Playgroud) 我需要结合令牌和 cookie 来授权 wepapi 项目中的请求。我添加了 Cookie 和 Jwt 来验证请求。在更改 DefaultPolicy 之前,我可以获得我的声明(/信息),但更改后我得到 401。
这是我的 Program.cs 代码:
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://localhost:7208/";
options.TokenValidationParameters.ValidateAudience = false;
options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };
});
var multiSchemePolicy = new AuthorizationPolicyBuilder(
CookieAuthenticationDefaults.AuthenticationScheme,
JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build();
builder.Services.AddAuthorization(o =>
{
o.DefaultPolicy = multiSchemePolicy;
});
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Run Code Online (Sandbox Code Playgroud)
和控制器代码:
namespace Whois.Api.Controllers
{
[ApiController] …Run Code Online (Sandbox Code Playgroud) asp.net-core ×6
c# ×4
.net-core ×2
asp.net ×2
asp.net-mvc ×2
jwt ×2
bearer-token ×1
openiddict ×1