MVC 5 External authentication with authentication mode=Forms

前端 未结 2 1386
我在风中等你
我在风中等你 2021-02-05 21:48

I\'m following this tutorial to create a simple MVC 5 app with external authentication. It\'s working fine, but, if I change the authentication mode=\"None\" to

2条回答
  •  名媛妹妹
    2021-02-05 22:41

    I was able to get this working (OWIN and FormsAuthentication) by adding Response.SuppressFormsAuthenticationRedirect = true to the ChallengeResult Class.

    If you are following the tutorial, here is the code:

    public class ChallengeResult : HttpUnauthorizedResult
    {
        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)
        {
            // this line did the trick
            context.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
    
            var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
            if (UserId != null)
            {
                properties.Dictionary[XsrfKey] = UserId;
            }
    
            context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
        }
    }
    

提交回复
热议问题