2013-07-23 27 views
0

我有一點麻煩與MVC HttpContext.User.Identity我得到system.security.principal.windowsIdentity,它給我我的Windows用戶名...我不希望這樣,我想要(首先)空User.Identity,所以我可以使用FormsAuthentication。我怎樣才能防止mvc使用這個system.security.principal.windowsIdentity來顯示正確的User.Identity?任何幫助,將不勝感激獲取正確的User.Identity

public ActionResult Index() 
    { 
     var s = HttpContext.User.Identity.Name; // 
     return View(); 
    } 

回答

2

在web.config文件的<system.web>部分,更改爲表單身份驗證。

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

工作!謝謝 –

0

檢查web.config以確保<authentication mode="Forms" />如果你沒有設置這個明確的,那麼你可能選擇的MVC模板內聯網當你第一次創建項目。

一旦配置文件更新,您還需要創建登錄/關閉操作和視圖。您可能需要使用Internet模板創建另一個項目並提取認證組件。

內網​​模板默認爲Windows身份驗證,而Internet模板默認爲表單。

1

這是因爲您啓用了Windows身份驗證。您可能與您的網絡服務器位於同一個域中,因此它會自動驗證您的身份。在您的配置中將其關閉或明確認證。

+0

是的!完全忘了!它的工作...非常感謝! –

+0

歡迎您Victor。 –