授权属性不起作用

时间:2011-07-26 20:19:37

标签: asp.net-mvc-3 login controller authorize-attribute

我想添加一个简单的登录信息。所以我认为最好的方法是在数据库中添加凭据,然后查询,如果用户名和密码算法你登录。这是有效的,它查询数据库,你登录并重定向到家。然后我尝试通过网址访问主页并注意到我可以在没有登录的情况下执行此操作。所以我想我应该使用

[Authorize]
Home Controller上的

属性,因为我不希望未经授权的用户访问它,因此应将其重定向回登录页面。这不起作用。当我在控制器上使用授权时,我在应用程序中出现错误。

Object reference not set to an instance of an object.

在web.config中,它看起来像这样:

<authentication mode="Forms">
  <forms loginUrl="~/Login/Index" timeout="2880" /> <-- I have changed the login url to my login controller.
</authentication>

我的登录控制器就像这样。

public ActionResult Index(UserModel model) <-- I query the db in the model.
    {
        if (!ModelState.IsValid)
        {
            return View(model);

        }

        if(!model.IsAdmin(model.UserName, model.Password))
        {  
            ModelState.AddModelError("username", "you are not a admin");
            return View(model);
        }
        FormsAuthentication.SetAuthCookie(model.UserName, false);

        return RedirectToAction("Index", "Home");

        }

那么使用此Authorize属性的正确方法如何?我可以按照我使用它的方式使用它吗?我在web.config中遗漏了什么吗? 此致!

对此进行了一些更新。由于它不起作用,我将其添加到web.config:

 <authentication mode="Forms">
        <forms loginUrl="~/Account/LogOn" timeout="5">
        </forms>
    </authentication>
    <membership defaultProvider="MyMembershipProvider">
        <providers>
            <clear/>
            <add name="MyMembershipProvider" type="MyNamespace.MyMembershipProvider"
                 enablePasswordRetrieval="false" 
                 enablePasswordReset="true"
                 requiresQuestionAndAnswer="false"
                 userIsOnlineTimeWindow="2" 
                 requiresUniqueEmail="false"
                 passwordFormat="Hashed"
                 maxInvalidPasswordAttempts="5" 
                 minRequiredPasswordLength="6" 
                 minRequiredNonalphanumericCharacters="0" 
                 passwordAttemptWindow="10"
                 applicationName="/" />
        </providers>
    </membership>

成员提供硬编码凭证:

public class MyMembershipProvider : MembershipProvider 
{
    public override bool ValidateUser(string username, string password)
    {
        if (username.Equals("user", StringComparison.CurrentCultureIgnoreCase) && password.Equals("myPassword"))
            return true;
        else
            return false;
    }

然后我尝试使用Authorization属性装饰我的HomeController,如下所示:

 [Authorize()]
public class HomeController : Controller
{}

但仍然得到同样的错误。我的意思是我可以登录,但是当我到达“Home”时,我会得到与以前相同的错误。这个名字是什么?!有什么线索吗?!

问候!

2 个答案:

答案 0 :(得分:0)

问题在于:

return RedirectToAction("Index", "Home");

您正在重定向到Home控制器的Index操作,该操作希望您传入某种类型的模型(不确定,因为您尚未发布Home控制器索引操作)。如果在未指定模型的情况下调用RedirectToAction,则在尝试访问该模型的任何元素时将导致错误,因为该模型将为null。这就是你得到

的原因
Object reference not set to an instance of an object.

当您使用null模型调用视图时,会发生这种情况。您需要更改重定向以包含控制器所期望的模型:

 return RedirectToAction("Index", "Home", SomeModel)

我认为您正在尝试正确使用[授权]。它只需要高于您试图锁定的Controller Action。 您应该发布Home控制器的Index操作,以获得有关您问题的更具体的答案。

答案 1 :(得分:0)

您是否为HomeController提供了所有代码?如果是这样,您将错过Home控制器的Index操作。 e.g

public class HomeController : Controller
{
    public ActionResult Index()
    {

        return View();
    }

}

现在你正在重定向到一个不存在的Action,它会给你一个错误。

如上所述,通过定义Index操作,您需要告诉控制器在调用Home控制器的Index操作时要执行的操作。您还需要添加一个View,告诉控制器在调用Index操作后要显示的页面。 (你的主页)

这个链接有一些非常好的教程http://www.asp.net/mvc让我开始使用MVC。它可能有助于进一步解释您正在做什么的错误。