从 startup.auth 到视图 ASP.NET 的 SteamAuth 变量

SteamAuth var from startup.auth to the view ASP.NET

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

但现在我想从登录用户那里获取 Steam 配置文件 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.) 在此处插入教程中的密钥:

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 在数据库 table 中保存了一个名为:'AspNetUserLogins' 的用户 steam id,table 中的 providerkey 是一个 url由更多的部分组成。例如:

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

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

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

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

请注意,您必须在 url 中添加步骤 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. )希望对大家有所帮助,更多问题欢迎追问!