首页 新闻 会员 周边

mvc 结合form身份验证的[AllowAnonymous]标记加不上?

0
悬赏园豆:10 [已解决问题] 解决于 2012-07-11 19:30

看微软的mvc3的登录模板示例里有[AllowAnonymous]的标记.我自己写就提示未能找到引用和命名空间,我看示例里的引用我都有了.

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

我自己写[AllowAnonymous]就报错,请问是什么原因?要怎么做才能正常的添加?

还有用sql创建表的时候怎么把列的注释也加进去?

``炯``的主页 ``炯`` | 初学一级 | 园豆:4
提问于:2012-07-08 16:36
< >
分享
最佳答案
0

[AllowAnonymous]在ASP.NET MVC 4中内置了,但在ASP.NET MVC 3中需要自己实现一下,比如:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class AllowAnonymousAttribute : Attribute
{
}

参考文章:Securing your ASP.NET MVC 3 application

 

收获园豆:10
dudu | 高人七级 |园豆:31003 | 2012-07-08 17:10

为什么在微软的mvc3模板中,没有看到AllowAnonymous的实现,直接打就有了?

``炯`` | 园豆:4 (初学一级) | 2012-07-09 20:54

@棋怜: 你选中AllowAnonymous,F12看一下它在哪里定义的

dudu | 园豆:31003 (高人七级) | 2012-07-09 21:28

@dudu: 转到定义是下面这样.自动定义的好像.我是不是需要自己手动的写一个这东西?

#region 程序集 System.Web.Mvc.dll, v4.0.0.0
// E:\Object\MvcDemo\packages\Microsoft.AspNet.Mvc.4.0.20505.0\lib\net40\System.Web.Mvc.dll
#endregion

using System;

namespace System.Web.Mvc
{
    // 摘要:
    //     Represents an attribute that marks controllers and actions to skip the System.Web.Mvc.AuthorizeAttribute
    //     during authorization.
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class AllowAnonymousAttribute : Attribute
    {
        // 摘要:
        //     Initializes a new instance of the System.Web.Mvc.AllowAnonymousAttribute
        //     class.
        public AllowAnonymousAttribute();
    }
}
``炯`` | 园豆:4 (初学一级) | 2012-07-10 23:07

@棋怜: 

程序集 System.Web.Mvc.dll, v4.0.0.0

这里是ASP.NET MVC 4.0,微软的示例程序应该是基于ASP.NET MVC 4.0的。

dudu | 园豆:31003 (高人七级) | 2012-07-11 07:39

@dudu: 好的谢谢.

``炯`` | 园豆:4 (初学一级) | 2012-07-11 19:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册