【问题标题】:SteamAuth var from startup.auth to the view ASP.NETSteamAuth var 从 startup.auth 到视图 ASP.NET
【发布时间】:2015-11-02 10:43:47
【问题描述】:

我想从 Steam 获取个人资料信息。所以首先我已经修复了我可以通过 Steam 登录的问题,我使用了这个教程:http://www.oauthforaspnet.com/providers/steam/

但现在我想从登录的用户那里获取 Steam 个人资料 ID,这样我就可以使用 Steam API 中的 JSON 从用户那里获取信息。

https://steamcommunity.com/profiles/(thisid)

希望有人能帮助我,我已经搜索了几个小时,但没有任何结果。

var options = new SteamAuthenticationOptions {
ApplicationKey = "Your API Key",
Provider = new OpenIDAuthenticationProvider // Steam is based on OpenID
{
    OnAuthenticated = async context =>
    {
        // Retrieve the user's identity with info like username and steam id in Claims property
        var identity = context.Identity;
    }
}}; app.UseSteamAuthentication(options);

【问题讨论】:

    标签: asp.net-mvc authentication steam-web-api


    【解决方案1】:

    不久前我们发现了答案:

    1.) 在此处插入教程中的密钥:

    var options = new SteamAuthenticationOptions
    {
        ApplicationKey = "Your API Key",
        Provider = new OpenIDAuthenticationProvider // Steam is based on OpenID
        {
            OnAuthenticated = async context =>
            {
                // Retrieve the user's identity with info like username and steam id in Claims property
                var identity = context.Identity;
            }
        }
    };
    app.UseSteamAuthentication(options);
    

    2.) 我们发现steam 正在将用户steam id 保存在名为“AspNetUserLogins”的数据库表中,该表中的providerkey 是由多个部分组成的url。例如:

    http://steamcommunity.com/openid/id/here-users-steamid
    

    我们只需要用户的 steamid,所以我们将在第 3 步中拆分它。

    3.) 制作一个控制器,例如:SteamController。这里我们要添加一个公共字符串:

    public string GetSteamID()
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new Steam.Models.ApplicationDbContext()));
            var CurrentUser = manager.FindById(User.Identity.GetUserId());
            if (User.Identity.Name != "")
            {
                string url = CurrentUser.Logins.First().ProviderKey;
                ViewBag.steamid = url.Split('/')[5]; //here we going to split the providerkey so we get the right part
            }
            else
            {
                ViewBag.steamid = "";
            }
    
            return ViewBag.steamid;
        }
    
    1. ) 现在我们可以添加一些东西,假设我们要添加个人资料信息。转到您的 SteamController 并添加:

          [HttpGet]
      public ContentResult GetProfile()
      {
          string url = string.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=addyourkeyhere&steamids={0}", this.GetSteamID());
          string result = null;
      
          using (var client = new WebClient())
          {
              result = client.DownloadString(url);
          }
          return Content(result, "application/json");
      }
      

    请注意,您必须在网址中添加步骤 1 中的 Steam 密钥。

    1. ) 创建一个名为:profile.js 的脚本。在这里,我们将添加我们的个人资料信息。

    function profilepic() {
        $.ajax({
            url: 'http://localhost:3365/steam/GetProfile',
            dataType: 'json',
            success: function (data) {
                $.each(data.response.players, function (key, value) {
                    if ($('.profile')) {
                        $('.profile').append("<img src='" + value.avatar + "'> <span>" + value.personaname + "</span>")
                    }
                    if ($('.profile1')) {
                        $('.profile1').append("<img src='" + value.avatarfull + "'>")
                    }
                    if ($('.username')) {
                        $('.username').append(value.personaname)
                    }
                    
                    console.log(value)
                });
            }, error: function (httpReq, status, exception) {
                console.log(status + " " + exception);
            }
        });
    }

    6.) 现在我们要做最后一步,用类创建一个视图,例如:

    <div class="userprofile">
        <span class="profile1"></span>
        <div class="userdescription">
            <h2 class="username"></h2>
        </div>
    </div>
    
    1. ) 我希望这对某些人有所帮助,如有更多问题,请随时提问!

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      相关资源
      最近更新 更多