12

Windows 인증을 사용하여 사용자를 인증하고 인증하는 MVC 4로 작성된 인트라넷 프로젝트가 있습니다.'다른 사용자로 로그인'MVC 4 Windows 인증

'다른 사용자로 로그인'기능을 추가해야합니다.

일부 검색 후 나는 401을 반환 제안 this solution을 발견하고 (양식 사용하여 호출되는) 다음과 같은 동작을 만들어 : 작업이 호출되는

// 
    // POST: /Home/LogOut 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult LogOut() 
    { 
     return new HttpUnauthorizedResult(); 
    } 

, 그리고 브라우저가 사용자 이름을 뜨고 암호 창은 결과로 다시 작업으로 리디렉션되므로 항상 401이 반환됩니다.

사용자가 새 자격 증명으로 로그인하면 어떻게 이전 작업으로 리디렉션합니까?

401을 반환하는 대신 서버 측에서 자격 증명을 무효화하는 방법이 있습니까?

+1

은 아마 당신은 수동으로 사용자 로그 아웃, 로그 아웃 방법에 반환 URL 인수를 전달하는 생각이 반환 URL로 로그온 리디렉션해야합니까? –

+0

Windows 인증을 사용할 때 '수동으로 사용자를 로그 아웃 하시겠습니까?' 내가 찾은 모든 솔루션은 403 응답을 브라우저에 보내야 자격 증명 상자가 표시됩니다. – Darbio

+0

미안하지만, 나는 FormsAuth가 아니라 WinAuth에 대해 이야기하고 싶다. 내가 찾은 유일한 해결책은 -하지만 그것을 테스트하지 - 자바 스크립트를 통해 로그 아웃하는 것입니다 : http://stackoverflow.com/questions/1067263/asp-net-windows-authentication-logout (나는 특별한 logpout 페이지에 반환 URL을 전달 뜻 위의 javascript 로그 아웃 함수를로드 할 때 호출 한 다음 리디렉션 - windows.location JS object - user를 사용하여 쿼리 문자열에서 returnurl 인수로 결정한 페이지로 이동 –

답변

22

사람들 reverse engineered\decompiled이 기능을 사용하는 Sharepoint의 일부 코드입니다.

나는 ASP.NET MVC 5 앱에서이를 테스트했으며 예상대로 작동합니다.

코드는 디 컴파일에서의 " 다른 사용자로 로그인"기능이있는 Microsoft.TeamFoundation.WebAccess을 기반으로합니다.

public ActionResult LogOut() 
{ 
    HttpCookie cookie = Request.Cookies["TSWA-Last-User"]; 

    if(User.Identity.IsAuthenticated == false || cookie == null || StringComparer.OrdinalIgnoreCase.Equals(User.Identity.Name, cookie.Value)) 
    { 
     string name = string.Empty; 

     if(Request.IsAuthenticated) 
     { 
      name = User.Identity.Name; 
     } 

     cookie = new HttpCookie("TSWA-Last-User", name); 
     Response.Cookies.Set(cookie); 

     Response.AppendHeader("Connection", "close"); 
     Response.StatusCode = 401; // Unauthorized; 
     Response.Clear(); 
     //should probably do a redirect here to the unauthorized/failed login page 
     //if you know how to do this, please tap it on the comments below 
     Response.Write("Unauthorized. Reload the page to try again..."); 
     Response.End(); 

     return RedirectToAction("Index"); 
    } 

    cookie = new HttpCookie("TSWA-Last-User", string.Empty) 
    { 
     Expires = DateTime.Now.AddYears(-5) 
    }; 

    Response.Cookies.Set(cookie); 

    return RedirectToAction("Index"); 

} 

출처 :이 방법은 항상 사용자를 로그 아웃 홈 페이지로 리디렉션됩니다

Force Sign in as a different user while using Windows Authentication in asp.net

+1

그레이트 발견!감사합니다 :) – Darbio

+0

그리고 우리는 어떻게 다른 행동으로 리디렉션합니까? –

+1

이것이 내가 인터넷을 좋아하는 이유입니다. 며칠 만에 저장되었습니다. 바로 여기! – HockeyJ

0

. 또한 모두가이 방법에 액세스 할 수 있도록 [AllowAnonymous]을 추가했습니다.

[AllowAnonymous] 
    public ActionResult LogOut() 
    { 
     HttpCookie cookie = Request.Cookies["TSWA-Last-User"]; 

     cookie = new HttpCookie("TSWA-Last-User", string.Empty) 
     { 
      Expires = DateTime.Now.AddYears(-5) 
     }; 
     Response.Cookies.Set(cookie); 

     Response.AppendHeader("Connection", "close"); 
     Response.StatusCode = 401; // Unauthorized; 
     Response.Clear(); 

     // redirect to home 
     Response.Write("<script type='text/javascript'>"); 
     Response.Write("var getUrl = window.location; var baseUrl = getUrl.protocol + " + 
      "'//' + getUrl.host + '/' + getUrl.pathname.split('/')[1]; window.location.href = baseUrl; "); 
     Response.Write("</script>"); 
     Response.End();   

     return RedirectToAction("Index"); 

    } 
+0

내 코드에 위의 기능을 추가 한 후 올바른 자격 증명을 제공 한 후에도 Windows 인증 창이 반복해서 나타납니다. – Sonali

+0

안녕하세요 @Sonali, 약 2 ~ 3 시간 후에 코드를 살펴보고 해결책을 찾으려고합니다. –

+0

같은 사용자와 계속해서 로그인하려고하기 때문에 이런 일이 발생합니다. 새 사용자를 시도한 후에도 정상적으로 작동했습니다. – Sonali