【问题标题】:MVC 4 Using Authorize with Custom Code Not WorkingMVC 4 使用授权和自定义代码不起作用
【发布时间】:2012-12-09 06:17:57
【问题描述】:

MVC 4 的新手。我不想做的是使用 MVC 4 附带的内置帐户管理。但是,我在 Views、AccountModel 和 AccountController 下创建了一个 Account 文件夹。

我想做的是限制对帐户文件夹中视图的访问。为此,在我的 AccountController 中,我使用以下内容:

[Authorize]
public class AccountController : Controller
{
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }
    public ActionResult bob()
    {
        return View();
    }...

在我的主页上,我有一个指向 Accounts 视图下的 bob 视图的链接,该链接现在将我重新路由到登录页面(这是正确的)。

现在,在提交表单后,使用正确的凭据(一切正常),我应该能够看到 bob,但由于我未获得授权,我被重定向回登录。代码:

    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            return RedirectToLocal(returnUrl);
        }...

我不想使用内置的连接到数据库,而是我需要根据字符串检查用户名,然后保持授权 = true 以便我可以查看 bob?

从长远来看,我计划连接到数据库并使用 SPROC 拉回信息,所以现在,我只想根据检查的字符串对用户进行身份验证。

【问题讨论】:

    标签: authorize


    【解决方案1】:

    在 ASP.net 看到 Forms Authenticated cookie 之前,您将继续被重定向。

    FormsAuthentication.SetAuthCookie(theUsersNameasString, boolForSessionOrPersistentCookie);
    

    假设您的 Web.Config 配置为表单身份验证

     <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    

    ASP.Net 将查找 .ASPXAUTH cookie,除非此 cookie 的名称在 WEB.CONFIG 中被更改

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-11
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2016-05-14
      • 1970-01-01
      相关资源
      最近更新 更多