Owin使用令牌身份验证托管Web应用程序

时间:2015-03-31 10:45:40

标签: owin nancy http-token-authentication

我正在尝试使用令牌身份验证创建一个owin Web应用程序,我的启动没有像

中的示例那样的任何特殊设置

https://github.com/NancyFx/Nancy/wiki/Token-Authentication

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
    {
        TokenAuthentication.Enable(pipelines, new TokenAuthenticationConfiguration(container.Resolve<ITokenizer>()));
    }
}

我只是

public void Configuration(IAppBuilder app)
{
    app.UseNancy();
}

我将模块定义为

public HomeModule(ITokenizer tokenizer)
{

    Post["/login"] = _ =>
    {
        DefaultUserIdentityResolver resolver = new DefaultUserIdentityResolver();
        //var userName = (string)this.Request.Form.Username;
        //var password = (string)this.Request.Form.Password;

        var claims = new List<string> { "admin", "poweruser" };

        var userIdentity = resolver.GetUser("ross", claims, Context);
        if (userIdentity == null)
        {
            return HttpStatusCode.Unauthorized;
        }

        var token = tokenizer.Tokenize(userIdentity, Context);

        return new
        {
            Token = token,
        };
    };
}

我知道的还不多,但是当我得到tokenize时,我得到Nancy.ErrorHandling.RouteExecutionEarlyExitException类型的异常,它在消息或堆栈跟踪中没有任何内容来指示问题。

我目前正在.NET 4.5.1上的casini中使用http进行托管

任何指针都将不胜感激

更新

消息是:

Exception of type 'Nancy.ErrorHandling.RouteExecutionEarlyExitException' was thrown.

Stack trace is:
   at Nancy.Authentication.Token.Tokenizer.Tokenize(IUserIdentity userIdentity, NancyContext context)
   at Samaritan.Hosting.HttpServices.HomeModule.<>c__DisplayClass11.<.ctor>b__7(Object _) in c:\src\DukeSoftware\Samaritan\Main\Samaritan.Hosting.HttpServices\HomeModule.cs:line 39
   at CallSite.Target(Closure , CallSite , Func`2 , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at Nancy.Routing.Route.<>c__DisplayClass4.<Wrap>b__3(Object parameters, CancellationToken context)

我尝试像这样设置startup.cs

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseNancy();
    }
}

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
    {
        TokenAuthentication.Enable(pipelines, new TokenAuthenticationConfiguration(container.Resolve<ITokenizer>()));
    }
}

但是我得到了以下异常

{"Located multiple bootstrappers:\r\n - Samaritan.Hosting.HttpServices.BootStarapper\r\n - Samaritan.Hosting.HttpServices.Bootstrapper\r\n\r\nEither remove unused bootstrapper types or specify which type to use."}

所以我删除了bootsrapper并离开了Startup。当您声明构造函数public HomeModule(ITokenizer tokenizer)时,似乎将实例化的tokenizer传递给模块 所以我并不认为令牌化器的创建是一个问题

1 个答案:

答案 0 :(得分:1)

你找到了解决办法吗?

我遇到了同样的例外。这是因为我在同一个目录中有2个EXE文件,其子级为&#39; DefaultNancyBootstrapper&#39;类。

我要使用旧的Nancy v1.0。所以似乎除了使用&#39; DefaultNancyBootstrapper&#39;之外别无他法。只在一个地方。