Я создал образец проекта в GitHub под названием AspNetMvcActiveDirectoryOwin. Вы можете разветвить его.
Есть несколько шагов, которые вы будете хотеть следующий -
Прежде всего, вы хотите проверить подлинность с Active Directory.
public class ActiveDirectoryService : IActiveDirectoryService
{
public bool ValidateCredentials(string domain, string userName, string password)
{
using (var context = new PrincipalContext(ContextType.Domain, domain))
{
return context.ValidateCredentials(userName, password);
}
}
public User GetUser(string domain, string userName)
{
User result = null;
using (var context = new PrincipalContext(ContextType.Domain, domain))
{
var user = UserPrincipal.FindByIdentity(context, userName);
if (user != null)
{
result = new User
{
UserName = userName,
FirstName = user.GivenName,
LastName = user.Surname
};
}
}
return result;
}
}
Во-вторых, вы хотите создать претензии, которые будут использоваться в промежуточном ПО 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);
}
}
короткий ответ с предоставленной информацией: Да. Возможно. – Shyju
@Shyju Спасибо за ваш ответ, ваш комментарий заставило меня понять, что я не спрашивал, как это сделать. Я уточню вопрос. – Lastwall
Я нашел следующее сообщение в блоге msdn https://blogs.msdn.microsoft.com/webdev/2013/07/03/understanding-owin-forms-authentication-in-mvc-5/, но это не похоже для работы с моей текущей версией ASP.NET MVC, поскольку методы не существуют для объектов, указанных в коде. – Lastwall