C# 4.0 OWIN外部登录,但持久化用户未进行身份验证

C# 4.0 OWIN外部登录,但持久化用户未进行身份验证,c#-4.0,asp.net-mvc-5,owin,C# 4.0,Asp.net Mvc 5,Owin,刚刚开始使用OWIN和ASP.NETMVC5 我希望在不保留用户数据的情况下让外部登录正常工作 目前,我可以从Facebook获得经过身份验证的响应,我可以看到所有声明,我甚至可以得到应用程序Cookie,但用户上下文中的用户与我登录的用户身份不同 以下是我目前掌握的代码: OWIN启动时 public class Startup { public void Configuration(IAppBuilder app) { app.UseCookieAuthent

刚刚开始使用OWIN和ASP.NETMVC5

我希望在不保留用户数据的情况下让外部登录正常工作

目前,我可以从Facebook获得经过身份验证的响应,我可以看到所有声明,我甚至可以得到应用程序Cookie,但用户上下文中的用户与我登录的用户身份不同

以下是我目前掌握的代码:

OWIN启动时

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            AuthenticationMode = AuthenticationMode.Passive,
            LoginPath = new PathString("/Login"),
            LogoutPath = new PathString("/Logout"),
        });
        app.UseExternalSignInCookie();
        var facebookAuthenticationOptions = new FacebookAuthenticationOptions
            {
                AppId = "XXX", AppSecret = "XXX",
            };

        facebookAuthenticationOptions.Scope.Add("email");

        app.UseFacebookAuthentication(facebookAuthenticationOptions);
    }
}
控制器

public class LandingPageController : Controller
{
    public ActionResult Index()
    {
        IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;

        //All I ever get is a WindowsPrincipal with an IsAuthenticated = false
        var identity = System.Web.HttpContext.Current.User as ClaimsPrincipal;
        var identity2 = ClaimsPrincipal.Current;
        var claimsPrincipal = authenticationManager.User ?? new ClaimsPrincipal();
        ...
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(string provider, string returnUrl = "/")
    {
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "LandingPage", new { loginProvider = provider, ReturnUrl = returnUrl }));
    }

    public async Task<RedirectResult> ExternalLoginCallback(string loginProvider, string returnUrl)
    {
        var authResult = await authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);

        authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

        authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, new ClaimsIdentity(authResult.Identity.Claims, DefaultAuthenticationTypes.ApplicationCookie));
            return Redirect(returnUrl);
    }

    public class ChallengeResult : HttpUnauthorizedResult
    {
        public ChallengeResult(string provider, string redirectUrl)
        {
            LoginProvider = provider;
            RedirectUrl = redirectUrl;
        }

        public string LoginProvider { get; set; }
        public string RedirectUrl { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = RedirectUrl }, LoginProvider);
        }
    }
公共类着陆页面控制器:控制器
{
公共行动结果索引()
{
IAAuthenticationManager authenticationManager=HttpContext.GetOwinContext().Authentication;
//我得到的只是一个带有IsAuthenticated=false的WindowsPrincipal
var identity=System.Web.HttpContext.Current.User作为ClaimsPrincipal;
var identity2=索赔当前;
var claimsPrincipal=authenticationManager.User??新建claimsPrincipal();
...
}
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果外部登录(字符串提供程序,字符串返回URL=“/”)
{
returnnewchallengeresult(provider,Url.Action(“ExternalLoginCallback”,“LandingPage”,new{loginProvider=provider,ReturnUrl=ReturnUrl}));
}
公共异步任务ExternalLoginCallback(字符串loginProvider,字符串returnUrl)
{
var authResult=wait authentication.authenticateSync(DefaultAuthenticationTypes.ExternalCookie);
authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
SignIn(newauthenticationproperties(){IsPersistent=false},newclaimsidentity(authResult.Identity.Claims,DefaultAuthenticationTypes.ApplicationOkie));
返回重定向(returnUrl);
}
公共类质询结果:HttpUnauthorizedResult
{
public ChallengeResult(字符串提供程序、字符串重定向URL)
{
LoginProvider=提供者;
重定向URL=重定向URL;
}
公共字符串登录提供程序{get;set;}
公共字符串重定向URL{get;set;}
公共覆盖无效ExecuteSult(ControllerContext上下文)
{
context.HttpContext.GetOwinContext().Authentication.Challenge(新的AuthenticationProperties{RedirectUri=RedirectUrl},LoginProvider);
}
}

当我使用Facebook登录时,我得到了一个应用程序Cookie,但上下文中的用户从未经过身份验证,我也没有得到我的声明。我错过了什么?

似乎我在两个Cookie配置上犯了错误。默认情况下,外部Cookie使用和的
被动
。我在设置Cookie身份验证时也使用了相同的设置。应用程序cookie需要使用
Active

将OWIN配置更改为以下设置使一切正常

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    AuthenticationMode = AuthenticationMode.Active,
    LoginPath = new PathString("/Login"),
    LogoutPath = new PathString("/Logout"),
});