2016-08-15 43 views
1

我正在使用第三方認證程序來授權我的南希網頁。我試圖在MVC中做到這一點,它是成功的,但我不能在南希重現相同的結果。南希認證MVC

下面是我在做什麼: MVC-啓動:

using Microsoft.Owin; 
using Owin; 
using Passport.Auth; 

[assembly: OwinStartup(typeof(TestAuthorization.Startup))] 
namespace TestAuthorization 
{ 
    public partial class Startup:StartupBase 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      base.Configuration(app); 
     } 
     public override string IdentityServerUri 
     { 
      get { return "https://test.ThirdParty.URL/identity"; } 
     } 
     public override string RedirectUri 
     { 
      get { return "https://localhost:4443"; } 
     } 
     public override string ApplicationClientId 
     { 
      get { return "local.fox.company"; } 
     } 
    } 
} 

南希啓動:

using Microsoft.Owin; 
using Owin; 
using Passport.Auth; 
using Nancy.Owin; 
[assembly: OwinStartupAttribute(typeof(AgreementManagementTool.Startup))] 
namespace AgreementManagementTool 
{ 
    public class Startup: StartupBase 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.UseNancy(); 
      base.Configuration(app); 
     } 
     public override string IdentityServerUri 
     { 
      get { return "https://test.ThirdParty.URL/identity"; } 
     } 
     public override string RedirectUri 
     { 
      get { return "https://localhost:4443"; } 
     } 
     public override string ApplicationClientId 
     { 
      get { return "local.fox.company"; } 
     } 
    } 
} 

現在,這裏是我的唯一南希的Program.cs:

class Program 
{ 
     static void Main(string[] args) 
     { 
      var uri = "https://+:4443"; //"https://localhost:4443"; 
      Console.WriteLine("Starting Nancy on " + uri); 

      using (WebApp.Start<Startup>(uri)) 
      { 
       Console.WriteLine("\n\nServer listening at {0}. Press enter to stop", uri); 
       Console.ReadLine(); 
       return; 
      } 
     } 
} 

現在我所要做的就是在我的Nancy模塊上寫[Authorize],它應該像MVC一樣工作。

MVC-控制器:

using System.Web.Mvc; 

namespace TestAuthorization.Controllers 
{ 
    [Authorize] 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      //this.RequiresMSOwinAuthentication(); 
      return View(); 
     } 
    } 
} 

南希模塊:

using Nancy; 
using AgreementManagementTool.Views.Home.Model; 
using System.Web.Mvc; 

namespace AgreementManagementTool.Modules 
{ 
    [Authorize] 
    public class HomeModule : NancyModule 
    { 
     public HomeModule() 
      : base("/home") 
     { 
      Get["/"] = parameters => 
      { 
       //this.RequiresMSOwinAuthentication(); // Not working 
       //this.RequiresAuthentication(); // Not working 
       HomeModel result = new HomeModel(); 
       result.ResultImport = "I am testing AUthentication"; 
       return View["index", result]; 
      }; 

     } 
    } 
} 

當我瀏覽網頁運行成功的MVC應用程序運行和授權工作全成之後,但南希亙古不顯示任何東西。 我試圖使用this.RequiresAuthentication();但它會拋出一個例外: Nancy-Exception

只是要提到,我不知道第三方身份驗證過程如何工作,我只需要使用它。 在MVC中,我已經收到了樣本,它工作正常,爲什麼它在南希工作不一樣。

+0

我可以上傳兩個項目,如果需要 –

回答

0

如果你只是使用南希,試試這個,以取代雙方你的創業(使用Owin.Security.Cookies):

using Microsoft.Owin.Security; 
using Microsoft.Owin.Security.Cookies; 

namespace TestOwin 
{ 
    public class Startup 
    { 
     // OWIN Configuration goes here 
     public static void Configuration(IAppBuilder app) 
     { 
      var cookieOptions = new CookieAuthenticationOptions() { AuthenticationMode = AuthenticationMode.Active }; 
      var nancyCfg = new NancyOptions(); 
      nancyCfg.Bootstrapper = new NBootstrapper(); 
      app.UseStaticFiles(); 
      app.UseCookieAuthentication(cookieOptions); 
      app.UseNancy(nancyCfg); 

     } 

    } 
} 

而在你NancyBootstrapper(在這裏你可以將用戶重定向到登錄頁面): 公共類NBootstrapper:DefaultNancyBootstrapper {// 南希引導程序覆蓋何去何從

protected override void ConfigureApplicationContainer(TinyIoCContainer container) 
    { 
     // We don't call "base" here to prevent auto-discovery of 
     // types/dependencies 
    } 
    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) 
    { 
     base.ConfigureRequestContainer(container, context); 

     // Here we register our user mapper as a per-request singleton. 
     // As this is now per-request we could inject a request scoped 
     // database "context" or other request scoped services. 
     container.Register<IUserMapper, UserValidator>(); 
    } 

    protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context) 
    { 
     // At request startup we modify the request pipelines to 
     // include forms authentication - passing in our now request 
     // scoped user name mapper. 
     // 
     // The pipelines passed in here are specific to this request, 
     // so we can add/remove/update items in them as we please. 
     var formsAuthConfiguration = 
      new FormsAuthenticationConfiguration() 
      { 
       RedirectUrl = "~/login", 
       UserMapper = requestContainer.Resolve<IUserMapper>(), 
      }; 

     FormsAuthentication.Enable(pipelines, formsAuthConfiguration); 
    } 

} 

然後你可以使用:

this.RequiresMSOwinAuthentication(); 

在您的模塊上。 希望這有助於!我的問題是試圖也獲得相同的身份驗證工作在南希內外,也與SignalR ...