ASP.NET MVC 5 Web API + Windows Authentication 方法
web.conf |
<configuration> … … <system.web> … … <authentication mode="Windows" /> </system.web> … … </configuration> |
\Controllers\UserController.cs |
using Newtonsoft.Json.Linq; [Authorize] public class UserController : ApiController { public JObject Get() { JObject obj; try { if (User.Identity.IsAuthenticated) { obj = new JObject(); obj.Add("Account", User.Identity.Name); obj.Add("Authentication", User.Identity.AuthenticationType); return obj; } else { obj = new JObject(); obj.Add("Error", "Not authenticated"); obj.Add("Authentication", User.Identity.AuthenticationType); } return obj; } catch(Exception ex) { obj = new JObject(); obj.Add("Error", ex.Message); return obj; } } } |