Active Directory로 인증한다고 가정합니다. 그렇다면 PrincipalContext 및 OWIN 미들웨어을 사용할 수 있습니다.
GitHub에서 AspNetMvcActiveDirectoryOwin이라는 샘플 프로젝트를 만들었습니다. 그것을 포크로 달아서 실행할 수 있습니다. 원래 ASP.Net MVC 용으로 작성되었지만 ASP.Net 웹 API에도 동일한 비즈니스 로직을 사용할 수 있습니다.
따라야 할 단계가 거의 없습니다. 우선, 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);
}
}
출처
2017-02-23 21:05:18
Win
이 ASP.Net Web Form 또는 ASP.Net MVC입니까? Active Directory를 사용하고 있습니까? – Win
웹 API를 사용하려고 했습니까? 분명히 IWA가 인증에 사용하는 유일한 것입니까? –