我参加聚会有点晚了,但是由于我偶然发现了这个试图解决我认为是相同问题的问题(使用 Live 对用户进行身份验证),所以我将描述我是如何让它工作的。
首先,ASP.NET 项目的正确 NuGet 包是LiveSDKServer。
接下来,获取用户信息是一个多步骤的过程:
- 将用户发送到 Live,以便他们可以授权您的应用访问他们的数据(其范围由您指定的“范围”确定)
- 使用访问代码实时重定向回您
- 然后,您使用访问代码请求用户信息
这在Live SDK 文档中得到了很好的描述,但我将在下面包含我非常简单的工作示例以将它们放在一起。管理令牌、用户数据和异常由您决定。
public class HomeController : Controller
{
private const string ClientId = "your client id";
private const string ClientSecret = "your client secret";
private const string RedirectUrl = "http://yourdomain.com/home/livecallback";
[HttpGet]
public ActionResult Index()
{
// This is just a page with a link to home/signin
return View();
}
[HttpGet]
public RedirectResult SignIn()
{
// Send the user over to Live so they can authorize your application.
// Specify whatever scopes you need.
var authClient = new LiveAuthClient(ClientId, ClientSecret, RedirectUrl);
var scopes = new [] { "wl.signin", "wl.basic" };
var loginUrl = authClient.GetLoginUrl(scopes);
return Redirect(loginUrl);
}
[HttpGet]
public async Task<ActionResult> LiveCallback(string code)
{
// Get an access token using the authorization code
var authClient = new LiveAuthClient(ClientId, ClientSecret, RedirectUrl);
var exchangeResult = await authClient.ExchangeAuthCodeAsync(HttpContext);
if (exchangeResult.Status == LiveConnectSessionStatus.Connected)
{
var connectClient = new LiveConnectClient(authClient.Session);
var connectResult = await connectClient.GetAsync("me");
if (connectResult != null)
{
dynamic me = connectResult.Result;
ViewBag.Username = me.name; // <-- Access user info
}
}
return View("Index");
}
}