2013-08-20 59 views
0
//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); 
    } 
} 

當此代碼在我的PC上本地運行時,它可以正常工作。我可以登錄並註銷,沒有任何問題,並且我在退出時無法訪問SystemController。正是我想要的。使用websecurity登錄只能在本地主機上運行

但是當我在我的域上發佈網站時,出於某種原因我無法登錄。它要求我窗口內的憑據:

Window

我不明白爲什麼這個彈出來首先,和所有的第二我只是希望用戶登錄與正規<input type="text">領域。

公共版本在數據庫中具有完全相同的表和數據。沒有什麼似乎是錯的。

編輯:

難道是因爲我沒有使用我的主機上的SSL?

EDIT2:

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

的伎倆。顯然你必須設置驗證模式:)

回答

1

這是因爲您在服務器上啓用了基本或Windows身份驗證。兩者都彈出對話框。您只需要表單身份驗證和匿名。

+0

如何禁用基本/ Windows身份驗證?對不起,我不熟悉這些。我會做一些研究。我的網站使用表單身份驗證。 – user1534664

+0

@ user1534664 - 您必須在服務器上配置IIS。既然你沒有告訴我們你的網站在哪裏,我們很難告訴你這是怎麼回事。 –

+0

好吧,你幫助我在正確的道路上:)我明白了! – user1534664

相關問題