小编Dav*_*son的帖子

具有Windows身份验证的ASP.NET Web API自主机

我试图使用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

23
推荐指数
1
解决办法
2万
查看次数

确定用户是否在.NET 4.0应用程序的AD组中

我试图确定用户是否是内部ASP.NET 4.0应用程序的Active Directory(AD)组的成员.在用户不是AD组成员的情况下,下面的代码在最后一行(return语句)上抛出"尝试访问已卸载的appdomain"异常错误.

public static bool IsInADGroup(string userName, string groupName)
{
    var principalContext = new PrincipalContext(ContextType.Domain);
    UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName);
    if (userPrincipal == null)
        return false;

    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
    if (groupPrincipal == null)
        return false;

      return userPrincipal.IsMemberOf(groupPrincipal);
}
Run Code Online (Sandbox Code Playgroud)

有关如何修复或其他解决方法的任何想法?

.net c# directoryservices userprincipal

14
推荐指数
2
解决办法
8168
查看次数