나는 이것을했다. 기본 아이디어는 양식의 기본 인증 양식입니다. 그러나 기본 로그인 페이지는 Windows 인증을 사용합니다. Windows 인증이 성공하면 Forms 티켓을 만들고 계속 진행합니다. 그렇지 않으면 로그인 페이지를 표시합니다.
유일한 경고는 Windows 인증이 항상 브라우저에 401 응답을 보내고 (Windows 자격 증명을 요구하는 경우), 도메인 사용자가 아닌 사용자는 항상 취소 버튼을 클릭해야하는 자격 증명 팝업을 받게된다는 것입니다.
내 프로젝트에서 MVC를 사용했습니다. 내 Windows 로그인 페이지는 /Login/Windows
이고 수동 로그인 페이지는 /Login
입니다. 여기
내 Web.config의 관련 영역입니다 : 여기
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Login/Windows" defaultUrl="~/" name=".MVCFORMSAUTH" protection="All" timeout="2880" slidingExpiration="true" />
</authentication>
<system.web>
<location path="Login">
<system.web>
<authorization>
<allow users="?" />
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="Login/Windows">
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
<httpErrors errorMode="Detailed" />
</system.webServer>
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>
내 인 LoginController입니다 :
[RoutePrefix("Login")]
public class LoginController : Controller {
[Route("")]
public ActionResult Login() {
//Clear previous credentials
if (Request.IsAuthenticated) {
FormsAuthentication.SignOut();
Session.RemoveAll();
Session.Clear();
Session.Abandon();
}
return View();
}
[Route("")]
[HttpPost]
public ActionResult TryLogin(string username, string password) {
//Verify username and password however you need to
FormsAuthentication.RedirectFromLoginPage(username, true);
return null;
}
[Route("Windows")]
public ActionResult Windows() {
var principal = Thread.CurrentPrincipal;
if (principal == null || !principal.Identity.IsAuthenticated) {
//Windows authentication failed
return Redirect(Url.Action("Login", "Login") + "?" + Request.QueryString);
}
//User is validated, so let's set the authentication cookie
FormsAuthentication.RedirectFromLoginPage(principal.Identity.Name, true);
return null;
}
}
로그인보기 그냥 POST를 수행하는 일반 사용자 이름/암호 형태가 될 것입니다 로그인하기.
이 시점에서 사용자는 수동으로 로그인하여 이동할 수있는 /Login
페이지가 있습니다. 사용자가 자동으로 리디렉션되는 기본 로그인 페이지 인 /Login/Windows
페이지도 있습니다. 그러나 Windows 로그인이 실패하면 일반 401 오류 페이지가 표시됩니다.
이처럼 매끄럽게 만드는 열쇠는 로그인보기를 사용자 정의 401 오류 페이지로 사용하는 것입니다. 나는 ViewRenderer class written by Rick Strahl을 사용하여 Application_EndRequest
에 응답 내용을 하이재킹하여이를 수행했습니다.
Global.asax.cs :
protected void Application_EndRequest(object sender, EventArgs e) {
if (Response.StatusCode != 401 || !Request.Url.ToString().Contains("Login/Windows")) return;
//If Windows authentication failed, inject the forms login page as the response content
Response.ClearContent();
var r = new ViewRenderer();
Response.Write(r.RenderViewToString("~/Views/Login/Login.cshtml"));
}
내가 찾은 또 다른주의 (나는 마지막 시도 이후이 버전 또는 두이었다 있지만)이 IIS Express에서 작동하지 않는다는 것입니다. IIS에서 설정하고 디버거를 가리 키도록했습니다.
코드를 입력하십시오. –