与ASP.NET MVC控制器一起使用的[Authorize]属性是否仅适用于已实现MembershipProvider的站点?
我对ASP.NET MVC缓存和授权感到困惑,并且急需一些澄清.
我的自制授权属性继承自AuthorizeAttribute.它的重写AuthorizeCore方法每次都会运行,即使我[OutputCache]在控制器操作上设置了一个属性.我明白那一部分.
现在,对我来说这个问题:现在当我实际执行输出缓存并且从缓存中提供页面时,每次AuthorizeCore都会失败.原因是当缓存请求时,httpContext.Session提供的AuthorizeCore是null!?这是一些简化的代码:
protected override bool AuthorizeCore(HttpContextBase httpContext) {
return (Session["userId"] != null)
}
Run Code Online (Sandbox Code Playgroud)
所以如果httpContext.Session是null,这显然每次都失败了.我需要访问会话,我还能如何检查请求是否被授权?这没有任何意义 - 如果这是它应该如何,那么我永远不能在ASP.NET MVC中使用缓存页面和身份验证.救命?
在我的MVC3项目中,我有一个带有[Authorize]属性的控制器.我有一个没有ajax的表单提交,如果他/她没有登录,它会将用户(如预期的那样)重定向到登录屏幕.
但是,现在我有一个用jquery ajax提交的表单,我怎么能做同样的事情呢?如果他/她未获得授权,请将用户重定向到登录屏幕?成功登录后,应将用户重定向到初始操作.
调节器
[Authorize]
[ValidateInput(false)]
public JsonResult SubmitChatMessage(string message)
{
if (!string.IsNullOrEmpty(message))
{
// Do stuff
}
// Return all chat messages
return GetChatMessages();
}
Run Code Online (Sandbox Code Playgroud)
客户端JQUERY
$(document).ready(function () {
$("form[action$='SubmitChatMessage']").submit(function (event) {
$.ajax({
url: $(this).attr("action"),
type: "post",
dataType: "json",
data: $(this).serialize(),
success: function (response) {
// do stuff
}
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
我可以从firebug控制台窗口看到,服务器返回:
GET http://domain/Account/LogOn?ReturnUrl=%2fProductDetails%2fSubmitChatMessage
Run Code Online (Sandbox Code Playgroud)
期待您的帮助!
更新可能的解决方案
我对这个框架是全新的,我仍然在学习它和C#的基础知识.与此同时,我在阅读书籍时遇到了Authorize和AllowAnonymous属性,我无法理解控制器如何"知道"用户是否正在尝试访问这些方法/操作.该信息存储在哪里?在执行登录方法时是否需要进行特殊处理?
谢谢你的帮助.
我有一些授权背后的行动方法,如:
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Create(int siteId, Comment comment) {
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我通过AJAX向Comment/Create with发送请求
X-Requested-With=XMLHttpRequest
Run Code Online (Sandbox Code Playgroud)
这有助于将请求标识为AJAX.当用户未登录并点击授权墙时,它将被重定向到
/Account/LogOn?ReturnUrl=Comment%2fCreate
Run Code Online (Sandbox Code Playgroud)
这打破了AJAX工作流程.我需要被重定向到
/Account/LogOn?X-Requested-With=XMLHttpRequest
Run Code Online (Sandbox Code Playgroud)
有什么想法可以实现吗?有什么方法可以获得对授权请求时会发生什么的更多控制?
我们的软件自动更新通过USB密钥安装(自动运行).如果我想确保只使用授权的USB密钥,那么最好的方法是什么?
我们的安装程序已经签名,否则将无法运行.但我更想检查已签名安装程序的USB密钥,如果它不在那里,只需忽略,甚至"弹出"USB设备.
而且我应该能够分辨出USB存储设备与相机或键盘之间的差异(代码中).
我只想禁用未经授权的存储设备.
谢谢你的想法.
我想在我的托管会话bean中保护特定角色的方法 "ROLE_ADMIN"
配置(的applicationContext-security.xml文件):
<global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"/>
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/**" access="isAuthenticated()"/>
<intercept-url pattern="/**" access="permitAll()"/>
<form-login
login-processing-url="/j_spring_security_check"
login-page="/login.jsf"
default-target-url="/main.jsf"
authentication-failure-url="/login.jsf" />
<session-management>
<concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
</session-management>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="user1" password="user1" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
<beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener"/>
Run Code Online (Sandbox Code Playgroud)
bean的安全方法:
@PreAuthorize("hasRole('ROLE_ADMIN')")
public String buy() {
...
}
Run Code Online (Sandbox Code Playgroud)
当我在网页下登录user1或anonym单击"购买"按钮时,它仍然会重定向到下一页.
我希望发生一些访问被拒绝的异常,但事实并非如此.
我目前有一个 MVC 应用程序,使用默认的“个人用户帐户”选项进行身份验证。这很好用,我能够根据用户角色创建用户、登录并限制对控制器的访问。
我有一个单独的 Web API 应用程序,目前不使用任何身份验证。
这两个应用程序都作为单独的站点托管在同一 IIS 服务器上。我想将来自 MVC 应用程序的身份验证合并到 Web API 应用程序中,因此在用户登录到 MVC 应用程序后,他们还可以根据用户角色访问 Web API 应用程序中的相应控制器。
从我一直在读,我需要设置<authentication mode='Forms'>在web.config这两个应用程序,因为他们是在同一台服务器上,验证自动在两者之间共享。我尝试[Authorize]在 Web API 应用程序的控制器中添加一个方法,通过 MVC 应用程序登录并尝试访问该方法,但从未输入该方法。
这就是我一直在看的:http : //www.bipinjoshi.net/articles/436bc727-6de2-4d11-9762-9ee1a1e3cbea.aspx
我在这里缺少什么?我希望有人能指出我正确的方向。
I'm using roles in my ASP.NET Web API 2 project to limit access to certain resources.
Now I have the following scenario: A clubmanager can only do a GET for a club that he manages. A clubmanager should not be authorized to access clubs that he does not manage.
This is the method that gets a club:
[Authorize(Roles = "ClubManager")]
[Route("{clubId}")]
public Club GetClub(int clubId)
Run Code Online (Sandbox Code Playgroud)
As you can see I only allow a user with the role "ClubManager" to access …
我正在将 Web API 从 .net Framework 迁移到 .net Core。如果应用程序在专用服务器上运行,旧版本能够忽略控制器上的授权属性。这是代码。我知道 .net core 3.1 不提供自定义 AuthorizeAttributes。这不是我的问题。
// A custom AuthroizeAttribute
public class ConditionalAuthorizeAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext httpContext)
{
if (environment_private())
return true;
else
return base.IsAuthorized(httpContext);
}
private bool environment_private()
{
// code that will tell you if you are in your dev environment or not
return Properties.Settings.Default.PrivateServer;
}
}
// How it was called from the controller
[ConditionalAuthorize(Roles = "MyRole")]
[Route(...)]
// More code for controller
Run Code Online (Sandbox Code Playgroud)
我只需要一种简单的方法来授权在我们的私人服务器上运行项目时的所有请求(由 …