我尝试使用Owin-self-hosting和Windows身份验证在NancyFx上设置服务。我已经按照一些示例但在尝试访问同一域中的服务时仍然获得Basic Auth登录提示。那里有其他实际可行的例子吗?或者我做错了吗?想念什么?
这里有一些代码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var listener = (HttpListener)app.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes =
AuthenticationSchemes.IntegratedWindowsAuthentication;
app.UseNancy();
}
}
public static void RequiresWindowsAuthentication(this NancyModule module)
{
module.Before.AddItemToEndOfPipeline(
new PipelineItem<Func<NancyContext, Response>>(
"RequiresWindowsAuthentication",
context =>
{
try
{
var env = ((IDictionary<string, object>)context.Items[Nancy.Owin.NancyMiddleware.RequestEnvironmentKey]);
var principal = (ClaimsPrincipal)env["server.User"];
if (principal == null || principal.Identity.IsAuthenticated == false) throw new UnauthorizedAccessException();
context.CurrentUser = new BasicUserIdentity {
UserName = principal.Identity.Name,
Claims = principal.Claims.Where(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role").Select(x => x.Value)
};
return null;
}
catch (Exception)
{
return HttpStatusCode.Unauthorized;
}
}));
}
public class IndexModule : NancyModule
{
public IndexModule(IIndexService indexService)
{
this.RequiresWindowsAuthentication();
Get["/secure"] = _ =>
{
return Context.CurrentUser.UserName;
};
}
}