English 中文(简体)
不工作的授权
原标题:Authorize attribute not working

我想添加一个简单的标识。 因此,我认为最好的办法是在数据库中添加全权证书,然后问,如果用户名和密码是你留下的。 这种做法是行之有效的,它质疑b,你被拖到家里,转往家里。 然后,我试图通过尿回家,并且注意到,我可以这样做,没有标识。 因此,我估计,我应当使用。

[Authorize]

由于我不想让未经许可的用户查阅,因此,应当重新引导到日志上。 这并不可行。 当我授权控制员使用时,我的申请有误。

Object reference not set to an instance of an object.

网上。 想:

<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");

        }

So how is the proper way to use this Authorize attribute? Can I even use it the way I m using it? Am I missing something in the web.config? Regard!

这方面的一些最新情况。 由于我没有工作,我增加了这个网络。

 <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;
    }

接着,我试图将我的家事主计长与授权权等同起来:

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

但这一错误仍然存在。 我指的是我可以 log,但当我到达“Home”时,我会发现与以前相同的错误。 地球名称是什么? 这对这一点有什么影响?

Regard!

问题回答

问题是:

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

你正在转向你的指数行动。 期望您采用某种模式的住户控制员(并非肯定是因为你贴上了家庭控制指数行动)。 当你称之为“反向行动”时,如果你试图利用这一模式的任何要素,就会产生错误,因为模型将无效。 正因为如此,你们才得到。

Object reference not set to an instance of an object.

当你称之为一种无效模式时,情况就发生了很多。 你们需要改变方向,以包括控制者期望的模式:

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

I think you are trying to use the [Authorize] correctly. It just needs to be above the Controller Action that you are trying to lock down. You should post the Index action of the Home controller to get some more specific answers on your problem.

贵国是否为本国主计长提供所有法典? 如果是,你就失去了家庭控制员的指数行动。 e.g

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

        return View();
    }

}

现在,你转向不存在的行动,给你留下错误。

你们需要告诉你的控制员,当家庭控制员的指数行动被称作“指数行动”时,我前面已经指出了这一点。 你们还需要补充一种观点,把在指数行动之后显示的那页数告诉控制者。 (家页)

This link has some really good tutorials http://www.asp.net/mvc that got me started with MVC. It may help further explain what is wrong with what you are doing.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签