【问题标题】:LiveAuthClient broken?LiveAuthClient 坏了?
【发布时间】:2014-01-07 10:40:29
【问题描述】:

当前版本的 LiveAuthClient 似乎已损坏或我的设置/配置中的某些内容已损坏。我通过 Package Manager Console 获得了 LiveSDK 版本 5.4.3499.620。

我正在开发一个 ASP.NET 应用程序,问题是 LiveAuthClient 类似乎没有用于身份验证的必要成员/事件,因此它基本上无法使用。

请注意 InitializeAsync 也拼写错误。

怎么了?

更新:

我获得了用于 ASP.NET 应用程序的另一个版本的 LiveSDK,但现在我每次尝试 InitializeSessionAsync 或 ExchangeAuthCodeAsync 时都会收到异常“找不到 id 为 1 的密钥”。

https://github.com/liveservices/LiveSDK-for-Windows/issues/3

我认为这不是解决问题的正确方法,但我目前没有其他选择。

【问题讨论】:

    标签: asp.net live-sdk


    【解决方案1】:

    我参加聚会有点晚了,但由于我偶然发现了这个试图解决我认为是相同问题的问题(使用 Live 验证用户),我将描述我是如何工作的。

    首先,ASP.NET 项目的正确 NuGet 包是 LiveSDKServer

    接下来,获取用户信息是一个多步骤的过程:

    1. 将用户发送到 Live,以便他们可以授权您的应用访问他们的数据(访问范围由您指定的“范围”决定)
    2. 使用访问代码实时重定向回您
    3. 然后您使用访问代码请求用户信息

    这在Live SDK documentation 中有很好的描述,但我将在下面包含我非常简单的工作示例以将它们放在一起。管理令牌、用户数据和异常由您决定。

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

    【讨论】:

    • 这看起来很棒。我还没有尝试过,但它看起来正是我过去 2 天一直在寻找的。谢谢贾里德。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多