2014-09-14 2 views
1

나는 자신의 IUserStore/etc를 구현하는 OWIN 인증을 사용하고 모든 것이 훌륭하게 작동하는 MVC 시스템을 사용하고 있습니다. 내 로그인을 외부 로그인과 연결하지 않고 MVC5와 함께 제공되는 코드를 사용하는 데 문제가 있습니다. 특히 : 그것은 AuthenticationManager에 대신 GetOwinContext 내 structuremap의 DI를 사용하도록MVC5 '링크 로그인'현재 아이덴티티를 잃고 외부 사이트

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult LinkLogin(string provider) 
     { 
      // Request a redirect to the external login provider to link a login for the current user 
      return new ChallengeResult(provider, Url.Action("LinkLoginCallback", "Manage",), User.Identity.GetUserId()); 
     } 

public class ChallengeResult : HttpUnauthorizedResult 
    { 
     // Used for XSRF protection when adding external logins 
     internal const string XsrfKey = "XsrfId"; 
     public ChallengeResult(string provider, string redirectUri) 
      : this(provider, redirectUri, null) 
     { 
     } 

     public ChallengeResult(string provider, string redirectUri, string userId) 
     { 
      LoginProvider = provider; 
      RedirectUri = redirectUri; 
      UserId = userId; 
     } 

     public string LoginProvider { get; set; } 
     public string RedirectUri { get; set; } 
     public string UserId { get; set; } 

     public override void ExecuteResult(ControllerContext context) 
     { 
      context.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true; 
      var properties = new Microsoft.Owin.Security.AuthenticationProperties() { RedirectUri = RedirectUri }; 
      if (UserId != null) 
      { 
       properties.Dictionary[XsrfKey] = UserId; 
      } 

      Container.GetInstance<Microsoft.Owin.Security.IAuthenticationManager>().Challenge(properties, LoginProvider); 
     } 
    } 

난 단지 도전의 결과를 변경했습니다. 문제는 모든 컨트롤러가 [CustomAuthorize] 특성으로 설정되어 있고 LinkLogin 메서드에 도달하면 정상적으로 작동한다는 것입니다. context.user.identity 변수는 AuthenticationType이 'ApplicationCookie'이며, 이는 내가 원하는 것입니다. 그러나 'LinkLogin'의 challegeresult 부분을 통과 할 때 CustomAuthorize 코드를 다시 치면 사용자는 더 이상 동일하지 않습니다. Context.User.Identity 변수는 이제 AuthenticationType 'Negotiate'이고, 대신 사용자 이름으로 Windows 로그인을 사용합니다.
이것은 내 응용 프로그램과 전혀 작동하지 않으며 올바른 쿠키 사용자로 해당 사용자를 해결할 수 없기 때문에 결국 403 오류가 발생합니다.

내가 여기서 잘못하고있는 것이 있습니까? 나는 AllowAnonymous를 LinkLogin 위에 올려 놓으려고했으나 전혀 도움이되지 않는다. 내 요구 사항에 더 잘 부합하도록 ChallengeResult를 수정해야하는지 잘 모르겠습니다. 어떤 도움이라도 좋을 것입니다, 고마워요!

답변

0

나는 '공급자'로서 우발적으로 null 값을 전달합니다. 일단 그것을 고쳐, 그것은 제대로 작동