AuthenticationManager.GetExternalLoginInfoAsync始终为空。
创始人
2024-09-22 14:31:46
0

在ASP.NET中,AuthenticationManager.GetExternalLoginInfoAsync方法返回外部登录信息。如果该方法始终返回空值,可能是由于以下几个常见问题导致的:

  1. 配置问题:确保在Startup.cs文件的ConfigureAuth方法中正确配置了外部登录提供程序。例如,如果你使用的是Google登录,应该添加以下代码:
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
    ClientId = "your_client_id",
    ClientSecret = "your_client_secret"
});
  1. 回调URL不正确:确保在外部登录提供程序的开发者控制台中设置了正确的回调URL。回调URL应该与你的应用程序的URL匹配,并且应该在URL末尾添加一个特定的路径,例如/signin-google

  2. 请求令牌被阻止:如果你的应用程序在防火墙或代理服务器后面运行,并且阻止了外部请求令牌,则可能会导致GetExternalLoginInfoAsync始终返回空值。确保你的服务器可以访问外部登录提供程序的令牌URL。

以下是一个完整的示例,演示如何正确地配置和使用AuthenticationManager.GetExternalLoginInfoAsync方法:

Startup.cs:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        // 配置应用程序的身份验证和授权
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });

        // 配置外部登录
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // 添加Google登录
        app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "your_client_id",
            ClientSecret = "your_client_secret"
        });
    }
}

AccountController.cs:

public class AccountController : Controller
{
    private ApplicationUserManager _userManager;
    private ApplicationSignInManager _signInManager;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager();
        }
        private set
        {
            _userManager = value;
        }
    }

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get();
        }
        private set
        {
            _signInManager = value;
        }
    }

    // 处理外部登录回调
    [AllowAnonymous]
    public async Task ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            // 处理无效的登录信息
            return RedirectToAction("Login");
        }

        // 根据外部登录信息执行用户登录或注册逻辑
        var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
            case SignInStatus.Failure:
            default:
                // 如果用户没有帐户,提示他们创建一个帐户
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
        }
    }

    // 辅助方法
    private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }
}

在上述示例中,ExternalLoginCallback方法使用AuthenticationManager.GetExternalLoginInfoAsync来获取外部登录信息。如果loginInfo为空,会重定向到登录页面或其他适当的操作。否则,根据外部登录信息执行相应的用户登录或注册逻辑。

希望以上解

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...