1

我有一个ASP.net核心(的.NET Framework 4.7)的应用程序正在使用像什么是this link如何在Cookie认证的同时使用asp.net核心Windows认证?

app.UseCookieAuthentication(new CookieAuthenticationOptions() 
    { 
     AuthenticationScheme = "CookieAuthentication", 
     LoginPath = new PathString("/Login/"), 
     AccessDeniedPath = new PathString("/Login/"), 
     AutomaticAuthenticate = true, 
     AutomaticChallenge = true 
    }); 

cookie认证我想要做的是允许与Cookie的使用Windows身份验证。因此,如果用户在公司的域中,则他/她不必输入用户名和密码。但是如果他们来自外部域,则他们被重定向到登录页面并输入他们的用户名和密码以被认证。

+0

您目前如何验证外部域用户? – Win

+0

我通过cookie认证来做到这一点。 – Baso

回答

1

如果用户在公司的域中,他/她不必输入 用户名和密码。

doesn't have to enter user name and password是棘手的部分。据我所知,你不能同时满足认证

但是,您可以问这两种类型的用户输入用户名和密码,并首先与您的系统进行身份验证。如果身份验证失败,则可以使用域帐户进行身份验证。

如果这种情况是可行的,您可以在ASP.NET Core中使用Novell.Directory.LdapHere是示例代码。

public bool ValidateUser(string domainName, string username, string password) 
{ 
    string userDn = $"{username}@{domainName}"; 
    try 
    { 
     using (var connection = new LdapConnection {SecureSocketLayer = false}) 
     { 
     connection.Connect(domainName, LdapConnection.DEFAULT_PORT); 
     connection.Bind(userDn, password); 

     if (connection.Bound) 
      return true; 
     } 
    } 
    catch (LdapException ex) 
    { 
     // Log exception 
    } 
    return false; 
} 

注:截至今天,的System.DirectoryServices是不是在ASP.NET核心产品尚未推出。

+0

我想我可以把app.UseCookieAuthentication放在app.UseMvc之后,并且启用windows auth和匿名auth,如果windows auth没有工作,管道将继续到达CookieAuthentication。在这段时间内允许我的登录控制器被访问[AllowAnonymous]并且控制器的其余部分具有[Authorize]属性。它有可能以任何方式? 谢谢, – Baso

+0

你可能可以把这两个中间件放在一起。我从来没有尝试过,所以我不能肯定地说。 – Win