C# 如何使用代码进行身份验证,而不使用集成的Windows身份验证显示登录提示?

C# 如何使用代码进行身份验证,而不使用集成的Windows身份验证显示登录提示?,c#,asp.net,authentication,windows-authentication,C#,Asp.net,Authentication,Windows Authentication,我正在尝试编写一个web应用程序,将用户重定向到使用集成Windows身份验证(IWA)的页面。我不希望使用以下对话框提示用户: 但是,我希望应用程序对用户进行身份验证,并将其重定向到页面 我正在尝试访问的URL没有连接到Active Directory-它实际上只是使用Windows身份验证本地凭据,并且正在访问的计算机上只有一个用户 我正在尝试让重定向到我的页面的所有人使用此1用户的凭据自动进行身份验证。将根据数据库中存在的用户会话访问凭据 IWA配置为使用NTLM。我假设您将使用Acti

我正在尝试编写一个web应用程序,将用户重定向到使用集成Windows身份验证(IWA)的页面。我不希望使用以下对话框提示用户:

但是,我希望应用程序对用户进行身份验证,并将其重定向到页面

我正在尝试访问的URL没有连接到Active Directory-它实际上只是使用Windows身份验证本地凭据,并且正在访问的计算机上只有一个用户

我正在尝试让重定向到我的页面的所有人使用此1用户的凭据自动进行身份验证。将根据数据库中存在的用户会话访问凭据


IWA配置为使用NTLM。

我假设您将使用Active Directory进行身份验证。如果是这样,您可以使用PrincipalContextOWIN中间件

我在GitHub创建了一个名为的示例项目。你可以用叉子叉它,然后运行它。它最初是为ASP.NETMVC编写的,但是您也可以为ASP.NETWebAPI使用相同的业务逻辑

您需要遵循以下几个步骤。首先,您需要使用Active Directory进行身份验证

注意:如果使用其他类型的身份验证方法,则需要修改此类中的逻辑

其次,您希望创建将在Owin中间件中使用的声明

public class OwinAuthenticationService : IAuthenticationService
{
    private readonly HttpContextBase _context;
    private const string AuthenticationType = "ApplicationCookie";

    public OwinAuthenticationService(HttpContextBase context)
    {
        _context = context;
    }

    public void SignIn(User user)
    {
        IList<Claim> claims = new List<Claim>
        {
            new Claim(ClaimTypes.Name, user.UserName),
            new Claim(ClaimTypes.GivenName, user.FirstName),
            new Claim(ClaimTypes.Surname, user.LastName),
        };

        ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignIn(identity);
    }

    public void SignOut()
    {
        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignOut(AuthenticationType);
    }
}
公共类OwinAuthenticationService:IAAuthenticationService
{
私有只读HttpContextBase\u上下文;
private const string AuthenticationType=“applicationcokie”;
public OwinAuthenticationService(HttpContextBase上下文)
{
_上下文=上下文;
}
公共无效登录(用户)
{
IList索赔=新清单
{
新索赔(ClaimTypes.Name、user.UserName),
新索赔(ClaimTypes.GivenName,user.FirstName),
新索赔(ClaimTypes.LastName、user.LastName),
};
索赔实体标识=新的索赔实体(索赔、认证类型);
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignIn(标识);
}
公共无效签出()
{
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignOut(AuthenticationType);
}
}

我假设您将使用Active Directory进行身份验证。如果是这样,您可以使用PrincipalContextOWIN中间件

我在GitHub创建了一个名为的示例项目。你可以用叉子叉它,然后运行它。它最初是为ASP.NETMVC编写的,但是您也可以为ASP.NETWebAPI使用相同的业务逻辑

您需要遵循以下几个步骤。首先,您需要使用Active Directory进行身份验证

注意:如果使用其他类型的身份验证方法,则需要修改此类中的逻辑

其次,您希望创建将在Owin中间件中使用的声明

public class OwinAuthenticationService : IAuthenticationService
{
    private readonly HttpContextBase _context;
    private const string AuthenticationType = "ApplicationCookie";

    public OwinAuthenticationService(HttpContextBase context)
    {
        _context = context;
    }

    public void SignIn(User user)
    {
        IList<Claim> claims = new List<Claim>
        {
            new Claim(ClaimTypes.Name, user.UserName),
            new Claim(ClaimTypes.GivenName, user.FirstName),
            new Claim(ClaimTypes.Surname, user.LastName),
        };

        ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignIn(identity);
    }

    public void SignOut()
    {
        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignOut(AuthenticationType);
    }
}
公共类OwinAuthenticationService:IAAuthenticationService
{
私有只读HttpContextBase\u上下文;
private const string AuthenticationType=“applicationcokie”;
public OwinAuthenticationService(HttpContextBase上下文)
{
_上下文=上下文;
}
公共无效登录(用户)
{
IList索赔=新清单
{
新索赔(ClaimTypes.Name、user.UserName),
新索赔(ClaimTypes.GivenName,user.FirstName),
新索赔(ClaimTypes.LastName、user.LastName),
};
索赔实体标识=新的索赔实体(索赔、认证类型);
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignIn(标识);
}
公共无效签出()
{
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignOut(AuthenticationType);
}
}

这是ASP.Net Web表单还是ASP.Net MVC?你在用Active Directory吗?我打算用web api?显然,IWA是他们用于身份验证的唯一工具?这是ASP.NETWeb表单还是ASP.NETMVC?你在用Active Directory吗?我打算用web api?显然IWA是他们唯一用于身份验证的东西?