c# - ASP Identity GetExternalLoginInfoAsync 总是返回 null

标签 c# asp.net-mvc openid asp.net-identity owin

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
     return RedirectToAction("Login");
}

loginInfo 始终为 null,我用 fiddler 检查响应,网站 (Steam) 似乎返回了正确的值

"response": {
        "players": [
            {
                "steamid": "76561198057961078",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "Press \"R\" to restart™",
                "lastlogoff": 1435947642,
                "commentpermission": 2,
                "profileurl": "http://steamcommunity.com/id/warheat1990/",
                "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/59/598fa035b19342a9e0b26a8115e8ddc5da0cc900.jpg",
                "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/59/598fa035b19342a9e0b26a8115e8ddc5da0cc900_medium.jpg",
                "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/59/598fa035b19342a9e0b26a8115e8ddc5da0cc900_full.jpg",
                "personastate": 1,
                "primaryclanid": "103582791434936111",
                "timecreated": 1327988764,
                "personastateflags": 0
            }
        ]

    }

那为什么我得到的是空值?我从 SO 中遇到同样问题的人那里读了一堆线程,但到目前为止没有运气。

我们将不胜感激。

最佳答案

所以我得到 null 的原因是因为我在 Startup.Auth 的 app.CreatePerOwinContext 之前放置了 app.UseSteamAuthentication("ABCDEFGHIJKLMNOPQRSTUVWXYZ");。 CS

public void ConfigureAuth(IAppBuilder app)
{
    app.UseSteamAuthentication("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); //if you put it here, it won't work
    // Configure the db context, user manager and signin manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

    //rest of the code here
}

所以我把它改成:

public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context, user manager and signin manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

    //rest of the code here
    app.UseSteamAuthentication("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); //works!
}

关于c# - ASP Identity GetExternalLoginInfoAsync 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216874/

相关文章:

database - 我需要在数据库中插入一个假行吗?

c# - 我可以将单个对象序列化为嵌套的 XML 吗?

c# - 检查存储过程是否返回值

c# - Web Api 在路由中排除消息处理程序

c# - 如何将 Razor View 模态属性值传递给 JavaScript 函数

ruby-on-rails - Authlogic OpenID 错误:未初始化的常量 OpenIdAuthentication::InvalidOpenId

asp.net-mvc - 像 Stack Overflow 这样的网站如何在 ASP.NET MVC 中传递用户信息?

c# - 是否可以有条件地序列化 CDATA?

c# - 尝试使用 WebAPI 时获取 "No type was found that matches the controller named ' SampleSlashBaseService'"

asp.net-mvc - Resharper 5 : How do I set the default formatting style for inline code blocks?