SteamAuth var从startup.auth到视图ASP.NET

时间:2015-11-02 10:43:48

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

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

但是现在我想从登录的用户那里获取steam profile id,这样我就可以使用steam API中的JSON来获取用户的信息。

https://steamcommunity.com/profiles/(this id)

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

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);

1 个答案:

答案 0 :(得分: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

我们只需要用户加速,所以我们将在步骤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");
    }
    
  2. 请注意,您必须在网址中添加第1步中的Steam键。

    1. )创建一个名为profile.js的脚本。在这里,我们将添加我们的个人资料信息。
    2. 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. )我希望这会帮助一些人,更多的问题,随时问!