记住LiveConnect凭据 [英] Remembering LiveConnect Credentials

查看:51
本文介绍了记住LiveConnect凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了一个应用程序,以使用LiveConnect SDK允许用户访问其SkyDrive帐户.他们第一次登录后,它会记住他们的帐户ID(他们的电子邮件),但是每次他们使用SignInButton控件离开页面时 下次,他们需要重新输入密码.我认为这可能会很烦人,具体取决于页面的使用频率.有没有一种方法可以防止用户每次都需要重新输入密码(也许我们需要一些特殊的值 存放在州内)?谢谢.


Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

I have recently updated an app to use the LiveConnect SDK to allow the user to access their SkyDrive account. After they login the first time, it remembers their account id (their email), but every time they leave the page with the SignInButton control on it, they need to reenter the password next time. I think this could be very annoying depending on how frequently the page is used. Is there a way to prevent the user from needing to reenter their password every time (maybe some special value we need to store in state)? Thanks.


Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

推荐答案

我使用手动方式向LiveConnect服务验证用户身份.我首先尝试使用LiveConnect库,但是发现它与它相处耗费太多时间,因此我采用了手动方法.

I use a manual way to authenticate the user to the LiveConnect services. I tried using the LiveConnect library first, but found it way too much time consuming to get along with it, so I went with the manual way.

这是怎么回事:

private void Authenticate_Click(object sender, RoutedEventArgs e)
        {
            if (Settings.LiveConnectAccessToken == string.Empty)
            {
                BrowserControl browser = new BrowserControl();
                browser.AuthenticationBrowser.Navigate(new Uri("https://oauth.live.com/authorize?"
                    + "&client_id=" + HttpUtility.UrlEncode(CLIENT_ID) // must not be here. Only for experimental perposes
                    + "&response_type=" + HttpUtility.UrlEncode("code")
                    + "&scope=" + HttpUtility.UrlEncode(ACCESS_SCOPE)
                    + "&redirect_uri=" + HttpUtility.UrlEncode("https://oauth.live.com/desktop")
                    + "&display=" + HttpUtility.UrlEncode("touch"),
                    UriKind.Absolute)
                );

                browser.AuthenticationBrowser.Visibility = Visibility.Visible;
                browser.AuthenticationBrowser.Navigated += new EventHandler<NavigationEventArgs>(AuthenticationBrowser_Navigated);

                popup = new Popup();
                popup.Child = browser;
                popup.IsOpen = true;
            }
            else
            {
                Settings.LiveConnectAccessToken = string.Empty;
                Settings.LiveConnectRefreshToken = string.Empty;
                Settings.LiveConnectCalendarID = string.Empty;

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    authenticationButton.Content = AuthenticationButtonContentLoginLocalized;
                });
            }
        }

如您所见,导航需要一个BrowserControl.这是我所做的:

As you can see you need a BrowserControl for the navigation. Here's what I did:

private void AuthenticationBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            DisableProgressIndicator();

            if (e.Uri.AbsoluteUri.ToLower().Contains("https://oauth.live.com/desktop"))
            {
                var query = (from pair in e.Uri.Query.Trim('?').Split('&')
                             let bits = pair.Split('=')
                             where bits.Length == 2
                             select new KeyValuePair<string, string>(bits[0], bits[1])).ToArray();

                var code = query.Where(kvp => kvp.Key == "code").Select(kvp => HttpUtility.UrlDecode(kvp.Value)).FirstOrDefault();

                if (string.IsNullOrEmpty(code))
                {
                    var error = query.Where(kvp => kvp.Key == "error").Select(kvp => HttpUtility.UrlDecode(kvp.Value)).FirstOrDefault();
                    var error_desc = query.Where(kvp => kvp.Key == "error_description").Select(kvp => HttpUtility.UrlDecode(kvp.Value)).FirstOrDefault();
                    MessageBox.Show("Error: " + error + "\n" + error_desc);
                    popup.IsOpen = false;
                    return;
                }

                // if you got the 'code' string here, this means, you got access.
        }

从现在开始,您可以随时根据需要对用户进行身份验证.但是请注意,访问令牌的寿命很短,因此每次访问实时服务之前,都需要先刷新令牌.

From now on, you can authenticate the user everytime to the services, whenever you want. But note that the access token has a very short life time, so everytime you need to refresh the token first, before you can access the live services.

这是验证用户身份的方式:

This is how you authenticate the user:

var request = HttpWebRequest.Create(new Uri("https://oauth.live.com/token?"
                        + "client_id=" + HttpUtility.UrlEncode(CLIENT_ID)
                        + "&client_secret=" + HttpUtility.UrlEncode(CLIENT_SECRET)
                        + "&redirect_uri=" + HttpUtility.UrlEncode("https://oauth.live.com/desktop")
                        + "&code=" + HttpUtility.UrlEncode(code)
                        + "&grant_type=" + HttpUtility.UrlEncode("authorization_code")
                        , UriKind.Absolute)
                    );

这是刷新访问令牌的方式:

and this is how you refresh the access token:

var request = HttpWebRequest.Create(new Uri("https://oauth.live.com/token?"
                + "client_id=" + HttpUtility.UrlEncode(CLIENT_ID)
                + "&client_secret=" + HttpUtility.UrlEncode(CLIENT_SECRET)
                + "&redirect_uri=" + HttpUtility.UrlEncode("https://oauth.live.com/desktop")
                + "&refresh_token=" + HttpUtility.UrlEncode(Settings.LiveConnectRefreshToken)
                + "&grant_type=" + HttpUtility.UrlEncode("refresh_token")
                , UriKind.Absolute)
            );

希望这会有所帮助.

-Dani


这篇关于记住LiveConnect凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆