我试图使用ASP.NET Web API自我主机选项与Windows身份验证,以便我可以确定登录用户,并最终根据用户的身份接受或拒绝用户.这是我的控制台应用程序代码:
using System;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace SelfHost
{
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://myComputerName:8080");
config.UseWindowsAuthentication = true;
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器:
[Authorize]
public class HelloController : ApiController
{
public string Get()
{
// This next line throws an null reference exception if the Authorize
// …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc windows-authentication self-hosting asp.net-web-api