2013-10-29 37 views
6

我試圖讓我的頭在使用OWIN。我創建了兩個MVC 5項目。一個使用Aspnet.Identity進行身份驗證,另一個開始爲空項目。MVC 5重定向到登錄頁面不與OWIN一起使用

添加以下到emptyp項目:

  • 與登錄操作帳戶控制器和視圖coresponding

  • Startup.cs與

另一部分Startup.cs
public partial class Startup 
{ 
     public void ConfigureAuth(IAppBuilder app) 
     { 
      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = "ApplicationCookie", 
       LoginPath = new PathString("/Account/Login") 
      }); 
     } 
    } 

我在Home控制器中用[作者ize]屬性。

當我在運行第一個項目並在登錄之前轉到關於屏幕,重定向到登錄操作。當我爲第二個項目做同樣的事情時,我得到一個「HTTP錯誤401.0 - 未經授權」而不是重定向。

任何想法會導致第二個行爲這樣?

+1

有比你在web.config兩個項目之間? –

+0

我已經比較了兩個web.config文件,但我會再次執行並報告回來...... – Jeff

+0

@AlexDresko,感謝您的幫助。我在昨天錯過的web.config中發現了一些不同之處。不幸的是,它並沒有幫助解決問題。 – Jeff

回答

5

我創建了兩個新的類似項目,並能夠重現您的錯誤。

在空白項目中,我必須安裝Microsoft.Owin.Host.SystemWeb(通過Nuget),一旦我這樣做了,我的Startup.cs類中就出現了一堆錯誤。結束了與此:

[assembly: OwinStartupAttribute(typeof(v2.Startup))] 
namespace v2 
{ 
    public partial class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      ConfigureAuth(app); 
     } 

     public void ConfigureAuth(IAppBuilder app) 
     { 
      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = "ApplicationCookie", 
       LoginPath = new PathString("/Account/Login") 
      }); 
     } 
    } 
} 

最後,我現在能打到/看到我的登錄視圖當我打電話飾有[Authorize]屬性關於()方法。

希望這會有所幫助! 文斯

+2

感謝您花時間嘗試此操作。我再次開始清理,終於找到了兩個不同的問題。在我嘗試這個項目的一個項目中,CookieAuthenticationOptions中有「AuthenticationMode = AuthenticationMode.Passive」。這是從我正在嘗試的其他東西中遺留下來的。在另一個項目中,我忘了從Startup.Configuration()中調用Startup.ConfigureAuth()!所以基本上兩個粗心的錯誤。我回到了正軌,並試圖找出下一個問題.... – Jeff

+0

nuget更新爲我解決了這個問題 – Scott

+0

這是一個錯誤呢? – Jakov

6

ASP.NET MVC 5 Web.config: "FormsAuthenticationModule" or "FormsAuthentication"

<system.webServer> 
    <modules> 
     <remove name="FormsAuthentication" /> 
    </modules> 
</system.webServer> 

額外的安全在我離開這兩個「錯字」處理器(如果微軟改變它以後給我)

<system.webServer> 
    <modules> 
    <remove name="FormsAuthenticationModule" /> 
    <remove name="FormsAuthentication" /> 
    </modules> 
</system.webServer> 
+0

這行代碼會做什麼 User

+0

點擊我解答的鏈接。我的答案也被刪除了,所以它之前沒有。 –