Ich habe ein Beispielprojekt bei GitHub AspNetMvcActiveDirectoryOwin erstellt. Sie können es abzweigen.
Es gibt nur wenige Schritte, die Sie folgende wollen -
Zunächst einmal wollen Sie mit Active Directory authentifizieren.
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;
}
}
Zweitens wollen Sie Ansprüche schaffen, die in Owin Middleware verwendet wird.
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);
}
}
kurze Antwort mit den von Ihnen angegebenen Informationen: Ja. Es ist möglich. – Shyju
@Shyju Danke für Ihre Antwort, Ihr Kommentar hat mir klar gemacht, dass ich nicht gefragt habe, wie ich es machen soll. Ich werde die Frage aktualisieren. – Lastwall
Ich habe den folgenden Beitrag auf dem MSDN-Blog https://blogs.msdn.microsoft.com/webdev/2013/07/03/understanding-owin-forms-authentication-in-mvc-5/ gefunden, aber es scheint nicht um mit meiner aktuellen Version von ASP.NET MVC zu arbeiten, da die Methoden nicht auf den Objekten vorhanden sind, auf die in dem Code verwiesen wird. – Lastwall