简体   繁体   English

检查用户是否登录

[英]Check if user has logged in or not

I have a gridview in which one column contains images. 我有一个网格视图,其中一列包含图像。 I have used lightbox to zoom the clicked image. 我已经使用灯箱缩放了单击的图像。 But when I right click on the image and select an option "Open link in new tab" then the image gets opened in a new tab. 但是,当我右键单击图像并选择一个选项“在新选项卡中打开链接”时,图像就会在新选项卡中打开。 that's not a problem. 那不是问题。

After that I press log out button. 之后,我按注销按钮。 Now I copy that image link and I paste it on the address bar, the same picture get's opened. 现在,我复制该图像链接,并将其粘贴到地址栏上,同时打开同一张图片。 I want that first it should be checked whether the user has logged in or not and then open the image if he has logged in otherwise not. 我希望首先应检查用户是否已登录,如果用户未登录则打开图像。 All the images of the gridview are stored in a folder named "product images". gridview的所有图像都存储在一个名为“产品图像”的文件夹中。

I am already checking login status on the page where gridview is used. 我已经在使用gridview的页面上检查登录状态。 Tell me what to do. 告诉我该怎么做。

Your problem is that security trimming is not applied on your images. 您的问题是安全修整未应用于图像。

Static resources do not follow the same route as an asp.net page so security trimming is not applied on images. 静态资源的路径与asp.net页面的路径不同,因此安全修整不会应用于图像。

As long as you have a web.config file that do not allow unauthorized users in that images folder, you could handle the problem by setting at web.config => system.webServer => modules 只要您有一个不允许在该图像文件夹中未经授权的用户的web.config文件,就可以通过在web.config => system.webServer => 模块中进行设置来解决该问题。

<modules runAllManagedModulesForAllRequests="true">

... but this would mean that all resources would be routed through the asp.net pipeline which would could generate performance issues. ...但是这意味着所有资源都将通过asp.net管道进行路由,这可能会导致性能问题。

In response to you question (my solution): 针对您的问题(我的解决方案):

I would actually go through another way, which would be a little more difficult, which would be: 我实际上将经历另一种方式,这将有些困难,这将是:

  • Make the folder product images invisible to any user by using iis Request Filtering (=> Hidden Segments => Disallow access to that folder) 通过使用iis请求过滤 (=> 隐藏的细分 => 禁止访问该文件夹),使文件夹产品图像对任何用户不可见。
  • Then create a custom http handled (.ashx file) in which I would have the image name as a parameter. 然后创建一个自定义的http处理(.ashx文件),其中将图像名称作为参数。 That handler at ProcessRequest would get the image parameter, open the specific file and stream the data from the image to response.) ProcessRequest的处理程序将获取image参数,打开特定文件,然后将数据从图像流式传输到响应。)
  • That handled could be easy under security trimming , as long as it goes through ASP.NET pipeline so users not logged in would not have access to the handler. 只要通过ASP.NET管道进行安全调整 ,该处理就很容易了,因此未登录的用户将无法访问该处理程序。 Performance would be some slower but only for those specific images. 性能会慢一些,但仅适用于那些特定的图像。 Also note that you should change any direct calls to the images. 另请注意,您应该更改对图像的任何直接调用。 For example, if you had src='/product images/imageA.png' , this should change as src='/ImagesHandler.ashx?image=imageA.png' . 例如,如果您有src ='/ product images / imageA.png' ,则应更改为src ='/ ImagesHandler.ashx?image = imageA.png'

After your comment on difficulty and Abhishek Punj answer I would like to mention: 在您对困难发表评论并回答Abhishek Punj之后,我想提及:

  • My solution wouldn't need to register handlers for each file type (what if you add a .jpg file type afterward and haven't registered it?). 我的解决方案不需要为每种文件类型注册处理程序(如果之后添加.jpg文件类型并且尚未注册该怎么办?)。

  • Also, even with Abhishek Punj answer you would still need to stream image data from the image file to response. 同样,即使使用Abhishek Punj回答,您仍然需要从图像文件中流式传输图像数据以进行响应。

  • In addition, with my solution, you won't need to custom check for user permissions at ProcessRequest but ASP.NET security trimming would handle it. 另外,通过我的解决方案,您无需在ProcessRequest上自定义检查用户权限,但是ASP.NET安全调整将处理该问题。

  • Also, my solutions tries to "protect the folder" where Abhishek Punj tries to "protect the file type globally". 另外,我的解决方案尝试“保护文件夹”,而Abhishek Punj尝试“全局保护文件类型”。

  • But most important , Abhishek Punj answer means that ALL IMAGES would go through the ASP.NET pipeline which means that if you had any images at log on form for example, they wont be shown to the user too! 但最重要的是 ,Abhishek Punj的回答意味着,所有图像都将通过ASP.NET管道进行处理,这意味着,例如,如果您在登录表单中有任何图像, 它们也不会显示给用户! After all, if you would hanlde all image file types, then why wouldn't you go with runAllManagedModulesForAllRequests="true", without any coding ? 毕竟,如果您要处理所有图像文件类型,那么为什么不使用runAllManagedModulesForAllRequests =“ true”,而不进行任何编码?

You can create a new handler and register it in IIS for the type of extension that your image file has. 您可以创建一个新的处理程序,并在IIS中针对您的图像文件具有的扩展名类型进行注册。 In that handler on begin request you can check if the user is authenticated in using the Principle set by the forms authentication. 在开始请求时该处理程序中,您可以使用表单身份验证设置的“原则”来检查用户是否已通过身份验证。 This would not fire for all the static files and hence would perform better than the solution mentioned by George. 这不会为所有静态文件触发,因此比George提到的解决方案性能更好。

However there is one more problem that you will face.. all the static resources are cached by the browser and hence it might not send in a request to the server at all and just display the image to the user without authentication. 但是,您将面临另一个问题。所有静态资源都由浏览器缓存,因此它可能根本不会向服务器发送请求,而只是在不进行身份验证的情况下向用户显示图像。

To deal with this problem you will need to set the no-cache header in the response from the custom handler you wrote in the first step so that the browser dosent cache the response and always hits the server to serve the response. 要解决此问题,您将需要在第一步中编写的自定义处理程序的响应中设置no-cache头,以便浏览器指定缓存响应并始终访问服务器以提供响应。

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
Response.AppendHeader("Expires", "0"); // Proxies.

Hope this gives you the desired direction. 希望这能给您期望的方向。

EDIT: Based on points raised by George 编辑:基于乔治提出的观点

runAllManagedModulesForAllRequests="true" will not only be triggered for all images it will also be triggerd for all CSS files, and javascript files as well.. hence increasing the overhead. runAllManagedModulesForAllRequests =“ true”不仅会为所有图像触发,还会为所有CSS文件和javascript文件触发。因此增加了开销。

If you want to specifically restrict files just in a specific directory a very simple way would be to mention the directory as a key in the config and do a regex comparison to check if the request is required to be authenticated or not.. this can also be extended to cater to various files or directories to be included or restricted with a custom configuration section as per need. 如果要只在特定目录中专门限制文件,一种非常简单的方法是将目录提到为config中的键,并进行正则表达式比较以检查是否要求对请求进行身份验证。.这也可以可以扩展以适应各种文件或目录,以根据需要通过自定义配置节包含或限制这些文件或目录。

As far as registering the handler for requests with other file extensions is considered its a matter of seconds.. not a painful task. 就注册具有其他文件扩展名的请求的处理程序而言,这仅是几秒钟的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM