Asp.net mvc 4 使用websecurity登录仅在本地主机上有效

Asp.net mvc 4 使用websecurity登录仅在本地主机上有效,asp.net-mvc-4,Asp.net Mvc 4,当这段代码在我的电脑上本地运行时,它可以完美地工作。我可以顺利登录和注销,并且在注销时无法访问SystemController。正是我想要的 但是当我在我的域名上发布网站时,由于某种原因我无法登录。它在窗口中询问我凭据: 我不明白为什么首先会出现这个弹出窗口,其次我只希望用户使用常规的字段登录 公共版本在数据库中具有完全相同的表和数据。这似乎没什么问题 编辑: //custom authorization public class RedirectAuthorize : AuthorizeAt

当这段代码在我的电脑上本地运行时,它可以完美地工作。我可以顺利登录和注销,并且在注销时无法访问SystemController。正是我想要的

但是当我在我的域名上发布网站时,由于某种原因我无法登录。它在窗口中询问我凭据:

我不明白为什么首先会出现这个弹出窗口,其次我只希望用户使用常规的
字段登录

公共版本在数据库中具有完全相同的表和数据。这似乎没什么问题

编辑:

//custom authorization
public class RedirectAuthorize : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
                filterContext.Result = new RedirectToRouteResult(new
                RouteValueDictionary(new { controller = "NotExist" }));            
        }
    }
}

[RedirectAuthorize]
public class SystemController : Controller
{
    public ActionResult Index()
    {   
        return View();
    }
}

// this method is inside AccountController
[HttpPost]
public ActionResult Login(User acc)
{
    if(ModelState.IsValid)
    {
        if(WebSecurity.Login(acc.username, acc.password))
            return RedirectToAction("Index", "System");
    }

    ModelState.AddModelError("IncorrectDetails", "Wrong details. Please try again.");
        return View(acc);
    }
}
可能是因为我没有在主机上使用SSL吗

Edit2:

//custom authorization
public class RedirectAuthorize : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
                filterContext.Result = new RedirectToRouteResult(new
                RouteValueDictionary(new { controller = "NotExist" }));            
        }
    }
}

[RedirectAuthorize]
public class SystemController : Controller
{
    public ActionResult Index()
    {   
        return View();
    }
}

// this method is inside AccountController
[HttpPost]
public ActionResult Login(User acc)
{
    if(ModelState.IsValid)
    {
        if(WebSecurity.Login(acc.username, acc.password))
            return RedirectToAction("Index", "System");
    }

    ModelState.AddModelError("IncorrectDetails", "Wrong details. Please try again.");
        return View(acc);
    }
}


成功了。显然,您必须设置身份验证模式:)

这是因为您在服务器上启用了基本身份验证或Windows身份验证。两者都会弹出对话框。您只需要表单身份验证和匿名身份验证。

如何禁用基本/Windows身份验证?对不起,我不熟悉这些。我要做些调查。“我的网站”使用表单身份验证。@user1534664-您必须在服务器上配置IIS。既然你没有告诉我们你在哪里托管你的网站,我们很难告诉你是怎么做到的。没关系,伙计,你帮了我一个大忙:)我弄明白了!