我很好奇你在元素中提供元素<allow>和<deny>元素的顺序是否重要?
我已经设置了使用下面的代码实现的自定义角色提供程序,除了它似乎从未被使用过,并且正在使用默认提供程序.当使用[Authorize(Roles = "Administrator")]属性装饰HomeController时,CustomRoleProvider正在调用构造函数(我只包含构造函数以查看是否会命中断点)但是没有调用任何方法.然后我留下了一个HTTP Error 401.0 - Unauthorized页面.
除了向web.config添加必要的位之外,我还没有做任何其他事情来使Windows身份验证工作.我认为它正在工作,因为如果我不包括<allow users="*"></allow>(显然没有包含Authorize属性)我得到一个401.2.: Unauthorized: Logon failed due to server configuration错误,所以我假设我正在被认证.
我已根据此SO帖子清除了我的浏览器cookie,但这没有任何效果.
CustomerRoleProvider
public class CustomRoleProvider : RoleProvider
{
public CustomRoleProvider()
{
}
public override bool IsUserInRole(string username, string roleName)
{
bool isUserInRole = false;
// omitted for brevity
return isUserInRole;
}
public override string[] GetRolesForUser(string username)
{
string[] roles = null;
// omitted for brevity
return roles;
}
// omitted …Run Code Online (Sandbox Code Playgroud) 我目前阻止匿名用户访问我的根应用程序.
/web.config
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)
但我允许匿名访问公共资源(图像,CSS等):
<location path="Images">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
现在,我想添加一个匿名用户可以访问的虚拟目录.我添加了一个基于Images路径的配置,但每当我尝试访问该位置时,我都会被重定向到登录页面,并将其ReturnURL设置为虚拟目录.
<location path="virtualDirectory">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
另外,我试图在我的虚拟目录的web.config中指定全局授权但是得到一个错误,说我只能有一次
/virtualDirectory/web.config:
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)
当我的根应用程序阻止匿名访问时,如何允许匿名访问虚拟目录?
我想创建一个自定义授权属性来检查角色和网址路径.
我已经找到了使用基于策略的授权在Asp.Net Core中执行此操作的方法,但我已经尝试实现它,但是我无法使用inclming url获取HttpContext.
AuthorizationHandlerContext 无法访问HttpContext可能.
如何通过url路径获取当前的HttpContext?是可以这样做还是用其他方式?
我已尝试使用此代码创建自定义策略:
public class RoleUrlValidationHandler : AuthorizationHandler<RoleUrlValidationRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RoleUrlValidationRequirement requirement)
{
var path = //Here I need get current url path for example - /api/posts/4545411
var pathPart = path.Split('/');
var clientId = pathPart[3];
if (context.User.IsInRole(clientId))
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建以下内容:
[Authorize(Policy="RoleUrlValidation")] //Get ClientId from Url and check User's roles
public class PostsController : Controller
{
public ActionResult Get()
{
}
}
Run Code Online (Sandbox Code Playgroud) 如何实现资源列表的授权?
我看到的所有文档都基于 IAuthorizationService 和 AuthorizeAsync 方法。但这仅适用于一种资源。
我是否应该检索所有资源,然后使用 IAuthorizationService 的 AuthorizeAsync 方法强制检查用户是否有权访问?这看起来非常笨拙、缓慢且低效。
你会怎么做?
asp.net-authorization .net-core asp.net-core asp.net-core-webapi
我用几个动作过滤器装饰了我的基本控制器.他们工作正常.
其中一个过滤器设置了请求 - 例如根据域设置文化等.
我还有一些需要使用Authorize属性进行授权的操作.
我的问题是,当用户尝试请求他们无权访问的页面时,授权过滤器会启动并将其重定向到一个页面,告诉他们他们无法访问该页面.
问题是动作过滤器从不运行,因此永远不会设置文化和其他请求数据.这有效地导致视图中的语言错误并且缺少其他数据.
我知道授权过滤器首先运行,但我的问题是:我如何设计这样,以便我可以确保在返回视图之前始终运行某些方法,无论授权如何.
希望有道理.
c# asp.net asp.net-mvc asp.net-membership asp.net-authorization
我尝试使用SPA开始使用asp.net核心Web应用程序.我已经通过教程构建了所有内容.所以我设置了这样的授权:
app.UseIdentity()
.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "MyCookieMiddlewareInstance",
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
Run Code Online (Sandbox Code Playgroud)
我有web-api控制器:
[Route("Somewhere")]
[Produces("application/json")]
[Authorize()]
public class MyControllerController : Controller
{
[HttpGet]
public async Task<IEnumerable<Something>> GetSomething()
{
//....
}
}
Run Code Online (Sandbox Code Playgroud)
和授权功能:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
{
//...
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe);
if (result.Succeeded)
{
_logger.LogInformation(1, "User logged in.");
return Redirect("somewhere");
}
//...
}
Run Code Online (Sandbox Code Playgroud)
但是当我在JS中调用我的webapi端点时,我会收到重定向到登录页面而不是401状态.
我已经开始进行调查,发现在计算器的答案,我要设置false到AutomaticChallenge并删除.UseIdentity().但是当我这样做时,我的[POST]AccountController.Login方法停止在线工作 - var result = …
c# asp.net asp.net-authorization asp.net-core-mvc asp.net-core
ASP.NET Core 2
帮我配置AddAuthentication两条路由:用户(用户帐户)和管理区域。
例如,如果用户未登录并尝试输入,则将/Account/Orders/其重定向到/Account/SignIn/。
但是,如果尝试访问的人/Admin/Orders/必须重新分配给/Admin/Signin/
尚未找到解决方案的ATM。
asp.net asp.net-authorization asp.net-core-mvc .net-core asp.net-core
我使用 Windows 身份验证创建了一个服务器端 Blazor 应用程序。它创建了以下文件。
登录Display.razor
<AuthorizeView>
Hello, @context.User.Identity.Name!
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)
但是,它显示“域\用户名”。它是一种显示用户名字的方法吗?
我试图打印所有的索赔类型。
@foreach(var c in context.User.Claims)
{
<p>@c.Value @c.Type</p>
}
Run Code Online (Sandbox Code Playgroud)
但是,只有一种类型带有名称。(价值为DOMAIN\username)
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
Run Code Online (Sandbox Code Playgroud) active-directory asp.net-authorization razor asp.net-core blazor
我有一个 AuthorizationHandler ,它依赖于为 .NET Core 3.1 的授权中间件提供异步方法的服务。我在方法内部调用了其中一些异步方法HandleRequirementAsync。整体代码如下所示:
{
public class MyAuthorizationHandler : AuthorizationHandler<MyRequirement, Tuple<string, string>>
{
private readonly IAuthIntelRepository authIntelRepository;
public UserAssistanceAuthorizationHandler(IAuthIntelRepository authIntelRepository)
{
this.authIntelRepository = authIntelRepository;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MyRequirement requirement, Tuple<string, string> someRessource)
{
//some async calls to authIntelRepository
if (/*someCondition*/false)
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
public class MyRequirement : IAuthorizationRequirement { }
}
Run Code Online (Sandbox Code Playgroud)
不过,一旦我使用await语句,我就会收到一个错误,表明签名未明确设置为异步。添加async到继承方法的签名会导致以下错误。
a return keyword must not be followed by an object expression. Did …
asp.net ×5
asp.net-core ×5
c# ×4
.net-core ×3
.net ×1
asp.net-mvc ×1
async-await ×1
blazor ×1
iis ×1
razor ×1
roles ×1