ASP.Net - 使用表单身份验证时,如何允许未经身份验证的用户查看导入的脚本文件?
我正在使用 ASP.Net 和表单身份验证。当用户被定向到登录页面时,我收到 JavaScript 错误:
消息:语法错误行:3 字符:1 代码:0 URI: http://localhost:49791/login.aspx?ReturnUrl=%2fWebImageButton.js< /a>
这是因为我在单独的 Web Control Project 控件中使用自定义图像按钮,该控件将 ScriptReference 添加到页面:
public class WebImageButton : LinkButton, IScriptControl, IButtonControl
{
protected override void OnPreRender(EventArgs e)
{
// Link the script up with the script manager
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
if (scriptManager != null)
{
scriptManager.RegisterScriptControl(this);
scriptManager.Scripts.Add(new ScriptReference("<snip>.WebImageButton.js", "<snip>"));
}
base.OnPreRender(e);
}
}
如果我将以下规则添加到我的 Web.Config 中,则文件将成功导入:
<location path="WebImageButton.js">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
这不是'不是很好,因为我有许多做同样事情的自定义控件,而且我并不特别喜欢单独验证它们的每个 js 文件。
有没有办法可以声明所有导入的脚本引用都应该被允许?我尝试授权 WebResource.axd 文件以防万一,但页面本身(呈现时)物理引用 WebImageButton.js 文件。
理想的情况如下:
<location path="My.WebControlLibraryProject.Controls">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
有没有办法在不列出每个文件的情况下实现这一目标?
EDIT: Just to be clear, these script files are in another project and are not in my actual web project. I know how to declare the location paths of directory paths to include a large number of files in one wack, but I can't figure out how to authenticate automatic script references, which are from embedded resources.
I'm using ASP.Net and forms authentication. When a user is directed to the Login Page I get a JavaScript error:
Message: Syntax error Line: 3 Char: 1
Code: 0 URI:
http://localhost:49791/login.aspx?ReturnUrl=%2fWebImageButton.js
This is because I am using a Custom Image Button in a separate Web Control Project control that adds a ScriptReference to the page:
public class WebImageButton : LinkButton, IScriptControl, IButtonControl
{
protected override void OnPreRender(EventArgs e)
{
// Link the script up with the script manager
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
if (scriptManager != null)
{
scriptManager.RegisterScriptControl(this);
scriptManager.Scripts.Add(new ScriptReference("<snip>.WebImageButton.js", "<snip>"));
}
base.OnPreRender(e);
}
}
If I add the following rule into my Web.Config, then the file is successfully imported:
<location path="WebImageButton.js">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
This isn't very good as I have a number of custom controls that do the same thing, and I don't particularly fancy authenticating each of their js files individually.
Is there no way that I can declare that all imported script references should be allowed? I tried authorising the WebResource.axd file in-case that allows it, but the page itself (when rendered) physically references the WebImageButton.js file.
The ideal scenario would be something like the following:
<location path="My.WebControlLibraryProject.Controls">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Is there any way to achieve this without listing each file?
EDIT: Just to be clear, these script files are in another project and are not in my actual web project. I know how to declare the location paths of directory paths to include a large number of files in one wack, but I can't figure out how to authenticate automatic script references, which are from embedded resources.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WebImageButton.js
是嵌入式资源吗?正如我所看到的,您已经实现了IScriptControl
。因此,您必须返回 IScriptControl.GetScriptReferences 上的所有脚本引用。我认为嵌入式资源永远不会被授权。我在不同情况下使用一些自定义控件,没有任何问题。我想知道脚本管理器直接引用WebImageButton.js
,而不是以 .axd 文件的形式。所以,我认为问题出在你的资源文件上。Is the
WebImageButton.js
an embedded resource? As I've seen, you had implementedIScriptControl
. Thus, you must return all of script references on theIScriptControl.GetScriptReferences
. I think an embedded resource never be authorized. I use some of custom controls in different situation and don't have any problem. I'm wondering that script manager references toWebImageButton.js
directly, and not in form of .axd file. So, I think the problem is arising from your resource file.这只是一个猜测 - 您是否尝试过在位置路径中放入通配符?也许类似于 <位置路径=“*.js”>?
This is just a guess - have you tried putting in a wildcard in the location path? Perhaps something like < location path="*.js">?
您可以指定一个通用位置并将所有脚本放在那里,以便所有不需要授权的脚本都会通过。您只需将路径更改为您想要的任何文件夹并将脚本放在那里。
这不仅适用于脚本,也适用于其他资源,例如图像、样式表等。
You can specify a generic location and put all your scripts there so that all the scripts that don't require authorization will pass. You just need to change the path to whatever folder you want it to be and put the scripts there.
That will work for not only scripts, but other resources as well, such as images, style sheets, etc.