首页 \ 问答 \ 角度评论指令 - 目的/意图/最佳实践(Angular Comment Directive - Purpose / Intent / Best Practice)

角度评论指令 - 目的/意图/最佳实践(Angular Comment Directive - Purpose / Intent / Best Practice)

阅读, http://www.w3schools.com/angular/angular_directives.asp我遇到了一个在注释中调用指令的例子,具体来说:

<!-- directive: w3-test-directive -->

您可以在http://www.w3schools.com/angular/tryit.asp?filename=try_ng_directive_comment中看到它的工作原理

这似乎对我很感兴趣。 有人可以描述你想要在评论中执行这样的事情时的目的或意图吗? 乍一看,它看起来像糟糕的设计,也许还有什么可以避免的? 如果我错了,我想了解何时使用的意图/目的和最佳实践。


Reading through, http://www.w3schools.com/angular/angular_directives.asp I came across an example where a directive is called in a comment, specifically:

<!-- directive: w3-test-directive -->

You can see it work in http://www.w3schools.com/angular/tryit.asp?filename=try_ng_directive_comment

This seemed concerning to me. Can someone please describe the purpose or intent on when you'd desire something in a comment to be executed like this? At first glance it looks like bad design and perhaps something to avoid? If I'm wrong, I'd like to understand the intent/purpose and best practice of when to use.


原文:https://stackoverflow.com/questions/37924755
更新时间:2021-09-11 18:09

最满意答案

通常这应该自动发生。 当匿名用户访问受保护的资源时(他是匿名的,因为他的会话过期),FormsAuthentication模块截获这些请求并通过追加指向受保护资源的ReturnUrl查询字符串参数重定向到您在web.config中注册的loginUrl

因此,例如,如果您将~/Account/LogOn配置为您的登录网址,并且匿名用户尝试点击~/Foo/Bar受保护的资源,他将被重定向到~/Account/LogOn?ReturnUrl=%2FFoo%2FBar

然后,他将看到一个登录页面,在那里他将输入他的凭据并将该表单提交给Account控制器上的[HttpPost] LogOn操作。 凭证将被验证,如果有效,将生成一个新的表单身份验证cookie,并将用户重定向到最初请求的受保护资源。

以下是LogOn操作的相应代码:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

请注意,成功验证后,用户是如何重定向到默认Home/IndexreturnUrl参数的。

当然,我在这里讲的所有这些故事都适用于由Visual Studio创建的默认ASP.NET MVC模板。 如果由于某种原因您修改了此模板,这可能会解释您正在观察的行为。

在任何情况下,从我的回答中要记住的事情是,如果您使用的是表单身份验证,则该模块将作为ReturnUrl查询字符串参数将最初请求的受保护资源传递到配置的登录页面。 因此,在成功验证后,您需要将用户重定向到此页面。


Normally this should happen automatically. When an anonymous user hits a protected resource (he is anonymous because his session expired), the FormsAuthentication module intercepts this requests and redirects to the loginUrl you registered in your web.config by appending a ReturnUrl query string parameter pointing to the protected resource.

So if for example you configured ~/Account/LogOn to be your logon url, and the anonymous user attempts to hit the ~/Foo/Bar protected resource he will be redirected to ~/Account/LogOn?ReturnUrl=%2FFoo%2FBar.

Then he will be presented with a login page where he will input his credentials and submit the form to the [HttpPost] LogOn action on the Account controller. The credentials will be validated and if valid, a new forms authentication cookie will be generated and the user redirected to the initially requested protected resource.

Here's the respective code from the LogOn action:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

Notice how the user is redirected either to the default Home/Index or to the returnUrl parameter upon successful authentication.

Now of course all this story that I told here is true for the default ASP.NET MVC template created by Visual Studio. If for some reason you have modified this template this might explain the behavior you are observing.

In any case the thing to remember from my answer is that if you are using Forms Authentication, the module will pass as ReturnUrl query string parameter the initially requested protected resource to the configured logon page. So it's up to you to redirect the user to this page upon successful authentication.

相关问答

更多
  • 是的,事实上,如果会话令牌永不过期,则CWE-384会识别出一个漏洞,如果会话需要很长时间才会过期,那么这违反了CWE-613 。 这两个CWE页面都很好地解释了这个漏洞。 我不知道这个应用程序的细节,但通常可以使用会话令牌立即进行身份验证,而无需输入用户名/密码。 Yes, in fact if a session token never expires it is a vulnerability is recognized by CWE-384 , If the session takes a real ...
  • 通常这应该自动发生。 当匿名用户访问受保护的资源时(他是匿名的,因为他的会话过期),FormsAuthentication模块截获这些请求并通过追加指向受保护资源的ReturnUrl查询字符串参数重定向到您在web.config中注册的loginUrl 。 因此,例如,如果您将~/Account/LogOn配置为您的登录网址,并且匿名用户尝试点击~/Foo/Bar受保护的资源,他将被重定向到~/Account/LogOn?ReturnUrl=%2FFoo%2FBar 。 然后,他将看到一个登录页面,在那里他 ...
  • 我已创建过滤器来检查会话,如果它已过期,我将重定向到登录页面。 这是我的下面的代码。 你做得很糟糕。 这不是好的做法。 您已经有一个名为AuthorizeAttribute的过滤器属性。 我建议使用它。 此外,请查看此链接 ,了解ASP.Net Identity是在您的应用程序中实现授权/身份验证的简便方法。 I have created filter to check session and if it is expired i am redirecting to login page. This is ...
  • 浏览器“记住我离开的地方”功能确实是问题所在。 删除该选项会导致我的cookie /会话出现“预期”行为。 The browser "remember where I left off" functionality was indeed the problem. Removing that option resulted in the "expected" behavior for my cookies/sessions.
  • 我在考虑使用变量的解决方案,并且根据该变量,您将显示或不显示弹出窗口。 我太懒了,实际上没有编写代码,但我发现了一个描述解决方案的链接: ASP.NET MVC授权属性启动模式? I was thinking of a solution that uses a variable, and depending on that variable, you would show or not a popup. I was too lazy to actually write the code, but I've ...
  • 对于会话超时,我使用'普通'Servlet API。 自定义javax.servlet.http.HttpSessionListener在web.xml中定义,在setMaxInactiveInterval中使用setMaxInactiveInterval (在[s]中)在会话集超时中setMaxInactiveInterval 。 我知道这是'老派',但很简单,对我有用。 如果你想从spring获得超时值,可以从session访问ServletContext。 ApplicationContext app ...
  • 最后: 我的具体问题是我的托管(Arvixe)为每个网站设置了100 MB的内存限制。 由于映射到数据库的复杂查询,使用EF(可能还有任何ORM)会增加内存使用量,因此我的网站使用的内存超过100 MB,导致应用程序池因100MB内存限制而重新启动,因此会话被重置。 解决方案: 选择你的MVC网站( 见这篇文章 ) 使用另一种方式来保持会话,可能通过将其存储在SQL数据库中。 升级到虚拟或专用服务器 对于我的具体情况,设置每个网站使用共享应用程序池临时解决了问题,而我按照第一个选项上的帖子来优化我的网站,所 ...
  • 我可以在这里看到两个单独的可解决问题,所以我将分别讨论它们。 以AJAX结果返回登录名 : 不是仅仅返回登录屏幕,而是返回一条表明会话已过期的消息。 如果你的AJAX收到这样的消息,它知道用户需要重新登录。此时,你可以做类似的事情 window.location = loginpageurlhere; 或者,您可以检查您的AJAX响应以查看它是否是预期的响应或者它是否为登录页面。 如果是登录页面,请使用上述建议。 如果用户闲置时间过长,则显示对话框 : 为此,您需要知道用户处于非活动状态的时间。 你可以用 ...
  • 你应该试试这个 @{ var conf = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath); var section = (System.Web.Configuration.SessionStateSection)conf.GetSection("system.web ...
  • 实施滑动到期。 将过期时间保持在某个合理的值 - 一天,两周,最多一周; 在每个请求(最简单)或以一定的时间间隔更新Cookie。 Implement sliding expiration. Leave the expiration time to some reasonable value - day, two, week max; renew the cookie on each request (simplest) or at certain intervals.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。