对于通过Facebook进行身份验证的用户,ServiceStack身份验证IsAuthenticated始终为false

时间:2019-09-17 14:27:21

标签: asp.net-core oauth servicestack

您好,我尝试使用servicestack提供的OAuth身份验证FacebookAuthProvider

 var AppSettings = appHost.AppSettings;
            appHost.Plugins.Add(new AuthFeature(() => new CustomUserSession(),
                new IAuthProvider[] {
                    new CredentialsAuthProvider(AppSettings),
                    new FacebookAuthProvider(AppSettings),
                    new GoogleAuthProvider(AppSettings)
                }));

我已经实现了自定义AuthEvent,并且可以访问授权的用户数据

enter image description here

但是在端点会话中发生RedirectUrl之后为空并且IsAuthenticated属性为false enter image description here

在同一时间从Controller我可以看到创建的会话已成功保存在Cache中 enter image description here

仅当用户尚未在浏览器中登录Facebook,但对于已经登录Facebook的用户,身份验证才能正常工作

我做错了什么? 预先感谢!

更新:

转播方案:

  1. 确保您没有在浏览器中登录Facebook(打开facebook.com并在需要时注销)
  2. 运行Web应用程序(使用模板mvcauth创建的项目)
  3. 尝试通过Facebook登录 3.1。第一次尝试将您重定向到https://www.facebook.com/login.php吗?用于指定您的Facebook帐户凭据的页面
  4. 第一次尝试后,您将返回到Web应用程序页面/ Account / Login#s = 1,但是AccountController.Login中的User.Identity.IsAuthenticated = false仍然是未授权的。
  5. 第二次尝试后,您将最终成功登录

在第一次尝试后,同时会创建带有用户数据的会话并将其保存在缓存中,这看起来是从页面“ https://www.facebook.com/login.php”重定向后创建的;这是在没有身份验证数据的情况下创建的新会话,但是对于第二次尝试,我们成功登录,也许问题的核心是从“ https://www.facebook.com/login.php”的重定向

1 个答案:

答案 0 :(得分:0)

为了能够在MVC中使用ServiceStack.Auth,反之亦然,您需要注册NetCoreIdentityAuthProvider,以提供ServiceStack Authenticated Sessions和ASP.NET Core的Claims Principal之间的集成,例如:

public void Configure(IAppHost appHost)
{
    var AppSettings = appHost.AppSettings;
    appHost.Plugins.Add(new AuthFeature(() => new CustomUserSession(),
        new IAuthProvider[] {
            new NetCoreIdentityAuthProvider(AppSettings), /* Use ServiceStack Auth in MVC */
            new CredentialsAuthProvider(AppSettings),     /* Sign In with Username / Password  */
            new FacebookAuthProvider(AppSettings),        
            new GoogleAuthProvider(AppSettings),          
            new MicrosoftGraphAuthProvider(AppSettings),  
        }));

    appHost.Plugins.Add(new RegistrationFeature()); //Enable /register Service

    //override the default registration validation with your own custom implementation
    appHost.RegisterAs<CustomRegistrationValidator, IValidator<Register>>();
}