- ããŒã1ïŒéå§ç¹
- ããŒã2ïŒé«åºŠãªã¹ãã©ãã·ã¥ã¹ã¯ãªãŒã³
- ããŒã3ïŒããã®å人ã®æ¯åã¹ã¿ã€ã«
- ããŒã4ïŒãŠãŒã¶ãŒããŒã¿ã®ååŸïŒä»»æïŒ
- ããŒã5ïŒããŒã¿ã¯ã©ãã«ä¿åããŸããïŒ
èè ã®ã¢ã¬ã¯ã»ã€ã»ããããã³ãã«åºãæž¡ããŸãã
ãŠãŒã¶ãŒèå¥ã¯ãææ°ã®ã¢ããªã±ãŒã·ã§ã³ãç¹ã«ã²ãŒã ã®åºç€ã§ãã è©äŸ¡è¡šãå®çžŸãæ°æãªã©ã®ç€ŸäŒçæ©èœã®ãªãã²ãŒã ãæ³åããããšã¯å°é£ã§ãã 瀟äŒçæ©èœã®ãªãã¢ããªã±ãŒã·ã§ã³ã§ã¯ãããšãã°ãããã€ã¹éã®åæããã»ã¹ãæ¹åããããã«ãèå¥ãå¿ èŠã§ãã ã»ãšãã©ã®å Žåããã®åé¡ã«é¢ããç§ã®ç 究ã®åºçºç¹ãšãªã£ãã®ã¯ããŸãã«åæã®åé¡ã§ããã
åUWPã¢ããªã±ãŒã·ã§ã³ã«ã¯å°çšã®ã¯ã©ãŠãã¹ãã¬ãŒãžïŒããŒãã³ã°ããŒã¿ïŒãããããŠãŒã¶ãŒãåãMSã¢ã«ãŠã³ãã䜿çšããŠããã°ãä»»æã®ããã€ã¹ããã¢ã¯ã»ã¹å¯èœãªããŒã¿ãä¿åã§ããŸãã æ®å¿µãªããããã®ã¹ãã¬ãŒãžã«ã¯ããã€ãã®é倧ãªå¶éããããŸãã
ãŸããã¯ã©ãŠãã«é 眮ã§ããããŒã¿ã®ãµã€ãºã«ã¯å¶éããããŸãã ã»ãšãã©ã®ã¿ã¹ã¯ã§ã¯ãå²ãåœãŠããããµã€ãºã§ååã§ãããããã¯äŸç¶ãšããŠå¶éã§ãã ããã«éèŠãªå¶éã¯ãããŒã¿ãã¯ã©ãŠãã«é 眮ããååã§ãã ãã®æ©èœã®å éšæäœã¢ã«ãŽãªãºã ã¯ãããšãã°ãçãšããå¶éãããã€ã³ã¿ãŒãããæ¥ç¶ãªã©ã®å€ãã®èŠå ãèæ ®ããæçµçã«ããã€ã¹éã®åæããã»ã¹ã«ã¯10å以äžãããã ãã§ãªãããŸã£ããä¿èšŒãããŸããïŒè©³çŽ°ã¯ãã¡ã ïŒã
ãã®ãããªå¶éã¯ç§ã®ã¿ã¹ã¯ã«ã¯ãŸã£ããé©ããŠããŸããããã®ãããåæããã»ã¹ã¯ç¬ç«ããŠå®è£ ããããšã«æ±ºå®ããŸãããæåã®åé¡ã¯ãåããã¬ãŒã€ãŒã«å±ããããã€ã¹ãèå¥ããããšã§ããã
å€ãã®ã²ãŒã ãšã¢ããªã±ãŒã·ã§ã³ã¯Facebookã®ãŠãŒã¶ãŒã¢ã«ãŠã³ãã䜿çšããŠåæããŸãããã»ãšãã©ã®å Žåãããã¯æ¢åã®ã¢ã«ãŽãªãºã ãMicrosoft Storeã«è»¢éããããã§ãããå®è£ ã®å©äŸ¿æ§ã®ããã§ã¯ãããŸããã ä»ã«ã¯ããã¬ãŒã€ãŒãç»é²ããå¿ èŠã®ããç¬èªã®ãµãŒãã¹ããããŸããããããäžå¿ èŠãªè€éãã§ãã çµæãšããŠãæãè«ççãªãœãªã¥ãŒã·ã§ã³ã¯ãã¡ããããŠãŒã¶ãŒãééããªãæã£ãŠããMicrosoftã¢ã«ãŠã³ãã䜿çšããããšã§ããããããªããšãWindowsã¹ãã¢ããã¢ããªã±ãŒã·ã§ã³ãè³Œå ¥ããŠã€ã³ã¹ããŒã«ããããšãã§ããŸããã æ®ã£ãŠããã®ã¯ãç°ãªãããã€ã¹ã§ãŠãŒã¶ãŒããŒã¿ãèå¥ããããã«ãŠãŒã¶ãŒããŒã¿ãååŸããããšã ãã§ãã
質åã詳ãã調ã¹ãŠã¿ããšãMicrosoftã¢ã«ãŠã³ãæ å ±ã¯Microsoft Live APIã䜿çšããŠååŸãããŠããããšãããããŸããã å®éããŠãŒã¶ãŒã«é¢ããããŒã¿ãåä¿¡ããã«ã¯ããŠãŒã¶ãŒã¯èªåã®ã¢ã«ãŠã³ãã«ãã°ã€ã³ããå¿ èŠããããã¢ã¯ã»ã¹ããŒã¯ã³ãåä¿¡ããå¿ èŠããããŸããããã«ããããŠãŒã¶ãŒã¯ããããèŠæ±ã§ããŸãã ãã®æé ã¯ã»ãã¥ãªãã£ã®ããã«äœæããããã®ã§ã OAuthèªèšŒæšæºã®äžéšã§ãããåå¿è åãã®ç¬ç«ããå®è£ ã¯è€éã§äžäŸ¿ã«æããŸãã
幞ããªããšã«ãUWPã®äœæè ã¯ãOAuthãåå¿è ã®éçºè ã«ããããå¯èœæ§ã®ããæããããç解ããMicrosoftã¢ã«ãŠã³ãïŒã ãã§ãªãïŒãæäœãããšã¬ã¬ã³ãã§ããªãåçŽãªæ¹æ³ãäœæããŸããã ãã®ããŒã«ã¯ãã¢ã«ãŠã³ããããŒãžã£ãŒWebã¢ã«ãŠã³ãããšåŒã°ããåŸã§æ€èšããŸãã
空ã®ãããžã§ã¯ããäœæããMainPage.xamlãã¡ã€ã«ã®ã¡ã€ã³ã°ãªããã次ã®XAMLã«çœ®ãæããŸãã
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Button x:Name="LoginButton" Content="" Click="LoginButton_Click" /> <TextBlock x:Name="UserIdTextBlock"/> <TextBlock x:Name="UserNameTextBlock"/> <Image x:Name="UserAvatarImage" Height="50"/> </StackPanel>
XAMLã¯éåžžã«åçŽã§ããã説æã¯äžèŠãªã®ã§ããã¿ã³ã¯ãªãã¯ã€ãã³ãã«ç§»åããŠã次ã®ã³ãŒãã貌ãä»ããŸãã
AccountsSettingsPane.Show()
Windows.UI.ApplicationSettingsåå空éã®ã€ã³ããŒããè¿œå ããããšãå¿ããªãã§ãã ãããè¿œå ããªããšããã®ã³ãŒãã¯æ©èœããŸããã
ããã§ãã¢ããªã±ãŒã·ã§ã³ãå®è¡ããŠçµæã確èªã§ããŸãã
空ã®ãŠã£ã³ããŠã¯æåŸ ãããã®ã§ã¯ãããŸããããæåã«å ¥åããå¿ èŠãããã®ã¯åãªãã·ã§ã«ã§ãã ããããããããããŒã ãã§åããŸããããã¯ããã®å Žåããšã³ããªå¯èœãªã¢ã«ãŠã³ãã®ãªã¹ãã§ãã
Web Account ManagerããŒã«ã¯éåžžã«æè»æ§ãããããããã®ã³ãã³ããæåã§äœæããããå¿ èŠãªãããã€ããŒããããŠã³ããŒãã§ããŸãã 2çªç®ã®ã±ãŒã¹ã§ã¯ãFindAccountProviderAsyncé¢æ°ã䜿çšãããŸããlogin.microsoft.comã¢ãã¬ã¹ã¯ã¢ã«ãŠã³ãã®ãããã€ããŒã§ãã
ãã ããåæåããåã«ããã«ã«ã³ãã³ããå ¥åããããšã¯äžå¯èœã§ãããããæåã«è¡ãå¿ èŠãããã®ã¯ã察å¿ããã€ãã³ãããµãã¹ã¯ã©ã€ãããããšã§ãã ããã«è¡šç€ºã³ãŒãã®åã«æ¬¡ã®è¡ãè¿œå ããããã«ã€ãã³ãã«ãã£ãŠåŒã³åºãããããã·ãŒãžã£ã圢æããŸãã
AddHandler AccountsSettingsPane.GetForCurrentView().AccountCommandsRequested, AddressOf BuildPaneAsync ⊠Private Sub BuildPaneAsync(sender As AccountsSettingsPane, e As AccountsSettingsPaneCommandsRequestedEventArgs) ' End Sub
ã¡ãªã¿ã«ãå ¬åŒã¬ã€ãã§ã¯ãAccountCommandsRequestedã€ãã³ããžã®ãµãã¹ã¯ãªãã·ã§ã³ã¯çŸåšã®ããŒãžãžã®ç§»è¡æã«çºçããããããåãæ¿ãããšãµãã¹ã¯ãªãã·ã§ã³ã解é€ãããŸãã ããã¯ãèªèšŒããã»ã¹çšã«å¥ã®ããŒãžãéžæããå Žåã«çã«ããªã£ãŠããŸããããã®ã¢ãããŒãã«ã¯ååãªæè»æ§ããªããšæããŸãã
BuildPaneAsyncãå¡ãã€ã¶ããŸãïŒ
Dim Deferral = e.GetDeferral Dim msaProvider = Await WebAuthenticationCoreManager.FindAccountProviderAsync("https://login.microsoft.com", "consumers") Dim command = New WebAccountProviderCommand(msaProvider, AddressOf GetMsaTokenAsync) e.WebAccountProviderCommands.Add(command) Deferral.Complete()
ãããŠããã«ãããã€ãã®è¿œå æé ãå®è¡ããŸãã
- ããã·ãŒãžã£ã«AsyncããŒã¯ãŒããè¿œå ããŸãã
- Windows.Security.Authentication.Web.Coreåå空éã®ã€ã³ããŒããè¿œå ããŸãã
- WebAccountProviderCommandåã®ã³ãã³ããã©ã¡ãŒã¿ãŒã䜿çšããŠGetMsaTokenAsyncããã·ãŒãžã£ãäœæããŸãã
ãã®ã³ãŒããæ±ããŸãã ãŸããã³ãã³ãã®ãªã¹ããååŸããã®ã«æéããããå Žåããããããããã«ããã£ã±ãã«ãªããŸã§ããã«ã®è¡šç€ºãé ãããå¿ èŠããããŸãã ãããè¡ãããã«ãé 延ãªããžã§ã¯ããäœæãããŸãã 次ã«ãåè¿°ã®FindAccountProviderAsyncé¢æ°ã䜿çšããŠããããã€ããŒãèªã¿èŸŒãŸããŸãã å€ããconsumersãã®ãã©ã¡ãŒã¿ãŒããã®é¢æ°ã«æž¡ãããããšã«æ³šæããŠãã ããã ããã¯ãçµç¹ã®ã¢ã«ãŠã³ãã§ã¯ãªããæšæºã®Microsoftã¢ã«ãŠã³ããååŸããããšããããã€ããŒã«äŒããŸãïŒãçµç¹ãã«ãªããŸãïŒã
ååŸãããããã€ããŒã䜿çšããŠæ°ããããŒã ãäœæããããããŒã ãã¯ãªãã¯ããããšãã«åŒã³åºãããããã·ãŒãžã£ãæå®ããçµæãããã«ã«è¿œå ããŸãã æåŸã®è¡ã¯ãããã«ã圢æãããŠè¡šç€ºã§ããããšãéç¥ããŸãã
ããã§ãèµ·åããŠããã°ã€ã³ããã¿ã³ãã¯ãªãã¯ãããšãããã€ãã®ãã°ã€ã³ãªãã·ã§ã³ã衚瀺ãããããã«ã衚瀺ãããŸãã ããã«ã«1ã€ã®ã³ãã³ããè¿œå ãããšããäºå®ã«æžæããããããŸãããã2ã€ã®ã³ãã³ãããããŸãã å®éããããç§ããã¢ã«ãŠã³ããããŒãžã£ãŒWebã¢ã«ãŠã³ããã奜ããã1ã€ã®çç±ã§ãã圌ã¯çŸåšã®ãŠãŒã¶ãŒã¢ã«ãŠã³ããç¬èªã«æ±ºå®ãããããã¡ã€ã³ãã°ã€ã³ãªãã·ã§ã³ãšããŠæäŸãããŠãŒã¶ãŒãå¥ã®ã¢ã«ãŠã³ãã䜿çšããå Žåãå¥ã®ãªãã·ã§ã³ã å éšããžãã¯ã®èŠ³ç¹ããèŠããšãäž¡æ¹ã®ãªãã·ã§ã³ã¯åäžã§ããããã®çµæãåã«äœæããGetMsaTokenAsyncããã·ãŒãžã£ã«éãããŸãã
ããã«ã¢ã¯ã·ã§ã³ãããã€ãã®æ®µéã«åããããšãã§ããŸãã ãŸããGetMsaTokenAsyncå ã§ãèŠæ±ããããŒã¿ã®çš®é¡ã瀺ãããŒã¿ãããã€ããŒãžã®èŠæ±ãè¡ãããã®èŠæ±ã®çµæãçæããå¿ èŠããããŸãã
Dim request As WebTokenRequest = New WebTokenRequest(command.WebAccountProvider, "wl.basic") Dim result As WebTokenRequestResult = Await WebAuthenticationCoreManager.RequestTokenAsync(request)
æåã®é¢æ°ã«æž¡ããããã©ã¡ãŒã¿ãŒãwl.basicãã«æ³šæããŠãã ããã ããã¯ãã¢ããªã±ãŒã·ã§ã³ããŠãŒã¶ãŒããŒã¿ãæäœãããšãã«åãåãã¢ã¯ã»ã¹èš±å¯ã瀺ããŠããŸãã ç°ãªãã¢ã¯ã»ã¹èš±å¯ãæå®ããããšã§ãã¢ãã¬ã¹åž³ãã«ã¬ã³ããŒãåçããã®ä»ã®å€ãã®ããŒã¿ãæäœã§ããŸãããååãã¢ãã¿ãŒããŠãŒã¶ãŒIDã®ã¿ãå¿ èŠãªã®ã§ãwl.basicã䜿çšããŸããããã§ååã§ãã èš±å¯ã®å®å šãªãªã¹ãã¯ã ããã«ãããŸã ã
ã¯ãšãªçµæãæ£åžžã«åä¿¡ããåŸãã¢ã«ãŠã³ãã¢ã¯ã»ã¹ããŒã¯ã³ã®ååŸã«åœ¹ç«ã€WebAccountåã®ãªããžã§ã¯ããäœæã§ããŸãã
If result.ResponseStatus = WebTokenRequestStatus.Success Then Dim account As WebAccount = result.ResponseData(0).WebAccount ApplicationData.Current.LocalSettings.Values("CurrentUserProviderId") = account.WebAccountProvider.Id ApplicationData.Current.LocalSettings.Values("CurrentUserId") = account.Id BackgroundConnectUser() End If
ãã®ã³ãŒããæ©èœãããã«ã¯ã Windows.Security.Credentialsããã³Windows.Storageåå空éãã€ã³ããŒãããå¿ èŠããããŸãã
åãåã£ãããŒã«ãŒã¯ãçŸåšã®ãŠãŒã¶ãŒã«é·æéé¢é£ããŠããŸããã€ãŸãããã°ã€ã³æé ãç¹°ãè¿ãç¹°ãè¿ãå¿ èŠã¯ãããŸããã 代ããã«ããããã®ããŒã¯ã³ãã¢ããªã±ãŒã·ã§ã³ã®ããŒã«ã«èšå®ã«ä¿åããŠãå°æ¥ã®ããã¯ã°ã©ãŠã³ãèªèšŒã«äœ¿çšã§ããããã«ããŸãã å®éãBackgroundConnectUserããã·ãŒãžã£ãåŒã³åºããŠããã¯ã°ã©ãŠã³ãèªèšŒã«é²ã¿ãŸãã
æåã«ãããã«ã³ãŒããæ©èœãããããã«å¿ èŠãªã€ã³ããŒããè¿œå ããŸãã
Imports System.Net.Http Imports Windows.Data.Json
BackgroundConnectUserã«ç§»åããŠãä¿åãããããŒã«ãŒãããŒãããŸãã
Dim providerId As String = ApplicationData.Current.LocalSettings.Values("CurrentUserProviderId") Dim accountId As String = ApplicationData.Current.LocalSettings.Values("CurrentUserId")
ããŒã¯ã³ã空ã§ãªãå Žåããããã€ããŒã圢æããããŒã¯ã³ãåãåãããã«ããããããªããžã§ã¯ããäœæãããªã¯ãšã¹ããå®è¡ããŸãã
Dim provider As WebAccountProvider = Await WebAuthenticationCoreManager.FindAccountProviderAsync(providerId) Dim account As WebAccount = Await WebAuthenticationCoreManager.FindAccountAsync(provider, accountId) Dim request As WebTokenRequest = New WebTokenRequest(provider, "wl.basic") Dim result As WebTokenRequestResult = Await WebAuthenticationCoreManager.GetTokenSilentlyAsync(request, account)
æåããå ŽåãAPIã¢ã¯ã»ã¹ããŒã¯ã³ãååŸããŠçŽæ¥ãªã¯ãšã¹ããå®è¡ã§ããŸãã
If result.ResponseStatus = WebTokenRequestStatus.Success Then Dim token As String = result.ResponseData(0).Token Dim restApi = New Uri("https://apis.live.net/v5.0/me?access_token=" + token) Dim client = New HttpClient() Dim infoResult = Await client.GetAsync(restApi) Dim Content As String = Await infoResult.Content.ReadAsStringAsync() Dim jsonO = JsonObject.Parse(Content) UserIdTextBlock.Text = jsonO("id").GetString UserNameTextBlock.Text = jsonO("name").GetString UserAvatarImage.Source = New BitmapImage(New Uri("https://apis.live.net/v5.0/me/picture?access_token=" + token)) End If
ãŠãŒã¶ãŒããŒã¿ãååŸããããã®ãªã¯ãšã¹ãã¢ãã¬ã¹ã¯ãMicrosoft Live APIã®ããã¥ã¡ã³ãã«èšèŒãããŠããŸãããäžè¬çãªããŒã¿ã®ãhttps://apis.live.net/v5.0/meããšãhttpsïŒ// apisãã®2ã€ã®ã¿ã«é¢å¿ããããŸãã live.net/v5.0/me/picture "ã¢ãã¿ãŒãååŸããŸãã ãŠãŒã¶ãŒããŒã¿ã¯Json圢åŒã§è¿ãããããã䟿å©ãªåæã®ããã«ããŒãµãŒã䜿çšãããŸãã
æåŸã«ãããã°ã©ã ãå®è¡ããŠããšã«ããäœãæ©èœããªãããšã確èªã§ããŸãã ããã¯ãMicrosoft Live APIãç¡æã§ããŒã¿ãæäŸããªãããã§ãã ãããã䜿çšããã«ã¯ãããŒã¿ã¢ã¯ã»ã¹èš±å¯ããã€ã³ããããMicrosoft LiveãµãŒãã¹ã®èå¥åãã¢ããªã±ãŒã·ã§ã³ã«å¿ èŠã§ãã 幞ããéçºè ã®æ å ±ããã«ã§æ°ããã¢ããªã±ãŒã·ã§ã³ãäœæãããšãèå¥åãèªåçã«çæãããŸãã ãã®èå¥åãçŸåšã®ãããžã§ã¯ãã«èªã¿èŸŒãã«ã¯ãWindowsã¹ãã¢ã«ãªã³ã¯ããã ãã§ãã ãããè¡ãã«ã¯ã[ãããžã§ã¯ã]> [ã¹ãã¢]> [ã¢ããªã±ãŒã·ã§ã³ãã¹ãã¢ã«é¢é£ä»ãã...]ã¡ãã¥ãŒã«ç§»åãããªã¹ãããç®çã®ååãéžæããŸãïŒãŸãã¯æ°ããååãç»é²ããŸãïŒã Microsoft Live APIã®å¿ èŠãªããŒã¿ã¯èªåçã«è»¢éããããããä»ã«äœãããå¿ èŠã¯ãããŸããã
ãããžã§ã¯ããåèµ·åããæçµçã«çµæã芳å¯ããŸãã
ããã«ã€ããŠã¯ãå ¬åŒã¬ã€ãããåã«è³æãæ¹åãããšããç®æšãèšå®ããã°ããŠãŒã¶ãŒããŒã¿ã®ååŸã«é¢ããæ€èšãå®äºããããšãã§ããŸãã
以äžã«ãå€æŽãå ããããå®å šãªãããžã§ã¯ãã³ãŒãã瀺ãããã®åŸãåå€æŽã«ã€ããŠèª¬æããæ£åœåããŸãã
XAMLïŒ
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel.Resources> <local:ConnectStatusToEnabledConverter x:Key="ConnectStatusToEnabledConverter"/> </StackPanel.Resources> <StackPanel Orientation="Horizontal"> <Button x:Name="LoginButton" Content="" Click="LoginButton_Click" IsEnabled="{Binding ConnectStatus, Converter={StaticResource ConnectStatusToEnabledConverter}, ConverterParameter=0}"/> <Button x:Name="LogOutButton" Margin="10,0,0,0" Content="" Click="LogOutButton_Click" IsEnabled="{Binding ConnectStatus, Converter={StaticResource ConnectStatusToEnabledConverter}, ConverterParameter=2}"/> </StackPanel> <TextBlock Margin="0,0,0,20" Text="{Binding ConnectStatus}"/> <TextBlock> : <Run FontWeight="Bold" Text="{Binding UserId}"/></TextBlock> <TextBlock> : <Run FontWeight="Bold" Text="{Binding UserName}"/></TextBlock> <Image Height="50" Source="{Binding UserAvatar}" HorizontalAlignment="Left"/> </StackPanel>
ãŠã£ã³ããŠã³ãŒã
Imports Windows.Security.Authentication.Web.Core Imports Windows.UI.ApplicationSettings Imports Windows.Security.Credentials Imports Windows.Storage Imports System.Net.Http Imports Windows.Data.Json Imports Windows.System Public NotInheritable Class MainPage Inherits Page Private CurUser As New UserManager Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded DataContext = CurUser End Sub Private Async Sub LoginButton_Click(sender As Object, e As RoutedEventArgs) If Not Await CurUser.BackgroundConnectUser Then CurUser.ConnectUser() End If End Sub Private Async Sub LogOutButton_Click(sender As Object, e As RoutedEventArgs) CurUser.LogOutUser(Await CurUser.GetWebAccount) End Sub End Class Public Class UserManager Implements INotifyPropertyChanged #Region " " Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged Private Sub OnPropertyChanged(PropertyName As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName)) End Sub #End Region Private UserIdValue As String Private UserNameValue As String Private UserAvatarValue As BitmapImage Private ConnectStatusValue As UserConnectStatusEnum = UserConnectStatusEnum.None #Region "" ''' <summary> ''' ID ''' </summary> ''' <returns></returns> Public Property UserId As String Get Return UserIdValue End Get Set(value As String) UserIdValue = value OnPropertyChanged("UserId") End Set End Property ''' <summary> ''' ''' </summary> ''' <returns></returns> Public Property UserName As String Get Return UserNameValue End Get Set(value As String) UserNameValue = value OnPropertyChanged("UserName") End Set End Property ''' <summary> ''' ''' </summary> ''' <returns></returns> Public Property UserAvatar As BitmapImage Get Return UserAvatarValue End Get Set(value As BitmapImage) UserAvatarValue = value OnPropertyChanged("UserAvatar") End Set End Property ''' <summary> ''' ''' </summary> ''' <returns></returns> Public Property ConnectStatus As UserConnectStatusEnum Get Return ConnectStatusValue End Get Set(value As UserConnectStatusEnum) ConnectStatusValue = value OnPropertyChanged("ConnectStatus") End Set End Property #End Region #Region " " ''' <summary> ''' ''' </summary> Public Sub ConnectUser() AddHandler AccountsSettingsPane.GetForCurrentView().AccountCommandsRequested, AddressOf BuildPaneAsync AccountsSettingsPane.Show() End Sub Public Async Sub LogOutUser(account As WebAccount) ApplicationData.Current.LocalSettings.Values.Remove("CurrentUserProviderId") ApplicationData.Current.LocalSettings.Values.Remove("CurrentUserId") Await account.SignOutAsync() UserId = "" UserName = "" UserAvatar = New BitmapImage ConnectStatus = UserConnectStatusEnum.None End Sub Public Async Function BackgroundConnectUser() As Task(Of Boolean) Dim result As Boolean = False Dim account As WebAccount = Await GetWebAccount() If account Is Nothing Then Return result ConnectStatus = UserConnectStatusEnum.Logon Dim request As WebTokenRequest = New WebTokenRequest(account.WebAccountProvider, "wl.basic") Dim requestResult As WebTokenRequestResult = Await WebAuthenticationCoreManager.GetTokenSilentlyAsync(request, account) If requestResult.ResponseStatus = WebTokenRequestStatus.Success Then Try Dim token As String = requestResult.ResponseData(0).Token Dim restApi = New Uri("https://apis.live.net/v5.0/me?access_token=" + token) Dim client = New HttpClient() Dim infoResult = Await client.GetAsync(restApi) Dim Content As String = Await infoResult.Content.ReadAsStringAsync() Dim jsonO = JsonObject.Parse(Content) UserId = jsonO("id").GetString UserName = jsonO("name").GetString UserAvatar = New BitmapImage(New Uri("https://apis.live.net/v5.0/me/picture?access_token=" + token)) ConnectStatus = UserConnectStatusEnum.Ssuccessful result = True Catch ex As Exception ConnectStatus = UserConnectStatusEnum.None End Try Else ConnectStatus = UserConnectStatusEnum.None End If Return result End Function #End Region #Region " , " ''' <summary> ''' ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Async Sub BuildPaneAsync(sender As AccountsSettingsPane, e As AccountsSettingsPaneCommandsRequestedEventArgs) RemoveHandler AccountsSettingsPane.GetForCurrentView().AccountCommandsRequested, AddressOf BuildPaneAsync Dim Deferral = e.GetDeferral Dim msaProvider = Await WebAuthenticationCoreManager.FindAccountProviderAsync("https://login.microsoft.com", "consumers") Dim command = New WebAccountProviderCommand(msaProvider, AddressOf GetMsaTokenAsync) e.WebAccountProviderCommands.Add(command) e.HeaderText = " Microsoft" Dim settingsCmd As SettingsCommand = New SettingsCommand("settings_privacy", " ", Async Sub() Await Launcher.LaunchUriAsync(New Uri("https://privacy.microsoft.com/ru-ru/")) End Sub) e.Commands.Add(settingsCmd) Deferral.Complete() End Sub ''' <summary> ''' ''' </summary> ''' <param name="command"></param> Private Async Sub GetMsaTokenAsync(command As WebAccountProviderCommand) ConnectStatus = UserConnectStatusEnum.Logon Dim request As WebTokenRequest = New WebTokenRequest(command.WebAccountProvider, "wl.basic") Dim result As WebTokenRequestResult = Await WebAuthenticationCoreManager.RequestTokenAsync(request) If result.ResponseStatus = WebTokenRequestStatus.Success Then Dim account As WebAccount = result.ResponseData(0).WebAccount ApplicationData.Current.LocalSettings.Values("CurrentUserProviderId") = account.WebAccountProvider.Id ApplicationData.Current.LocalSettings.Values("CurrentUserId") = account.Id Await BackgroundConnectUser() Else ConnectStatus = UserConnectStatusEnum.None End If End Sub ''' <summary> ''' ''' </summary> ''' <returns></returns> Public Async Function GetWebAccount() As Task(Of WebAccount) Dim providerId As String = ApplicationData.Current.LocalSettings.Values("CurrentUserProviderId") Dim accountId As String = ApplicationData.Current.LocalSettings.Values("CurrentUserId") If (providerId Is Nothing And accountId Is Nothing) Then Return Nothing End If Dim provider As WebAccountProvider = Await WebAuthenticationCoreManager.FindAccountProviderAsync(providerId) Dim account As WebAccount = Await WebAuthenticationCoreManager.FindAccountAsync(provider, accountId) Return account End Function ''' <summary> ''' ''' </summary> Public Enum UserConnectStatusEnum ''' <summary> ''' ''' </summary> None = 0 ''' <summary> ''' ''' </summary> Logon = 1 ''' <summary> ''' ''' </summary> Ssuccessful = 2 End Enum #End Region End Class Public Class ConnectStatusToEnabledConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert Return CStr(parameter).IndexOf(CStr(value)) > -1 End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack Throw New NotImplementedException() End Function End Class
ããŒãžã¬ã€ã¢ãŠãã¯ããã»ã©å€§ãã調æŽãããŠããŸããã æãéèŠãªéãã¯ãã¢ã«ãŠã³ãã®çµäºãã¿ã³ãšãæ¥ç¶ç¶æ ãç£èŠããè¡ãè¿œå ãããŠããããšã§ãã æ®ãã®å€æŽã«ãããããŒã¿è¡šç€ºãããèŠããããªããŸãããæçµçã«ããŒãžã®ã¿ã¹ã¯ã¯ãäž»ãªå€æŽãè¡ãããã³ãŒãå®è¡ã®çµæã瀺ãããšã§ãã ããããèæ ®ããŠãã ããã
ãŸããå¥ã®ã¯ã©ã¹ã§Webã¢ã«ãŠã³ããããŒãžã£ãŒãæäœããããžãã¯ãå®å šã«åãé€ããŸããã ããã¯ãã¢ããªã±ãŒã·ã§ã³å ã®ã©ãããã§ããã®ã³ãŒãã«ã¢ã¯ã»ã¹ã§ããããã«ããããã«è¡ãããŸãã ããšãã°ãã é«åºŠãªã¹ãã©ãã·ã¥ã¹ã¯ãªãŒã³ ããšããèšäºã§ãã¹ãã©ãã·ã¥ã¹ã¯ãªãŒã³å ã§å®è¡ããã®ãçã«ããªã£ãŠããé·ãæäœã«ã€ããŠèª¬æããŸãããBackgroundConnectUserã§ãŠãŒã¶ãŒããŒã¿ãååŸããããã»ã¹ã¯ããŸãã«ãã®ãããªæäœã§ãã
ã³ãŒããå¥ã®ã¯ã©ã¹ã«ç§»åããçµæãããŒãžèªäœã®ã¯ã©ã¹ã«ã¯ã»ãšãã©äœãæ®ããŸããã§ããã ããŒãžå ã®ãã¹ãŠã®äœæ¥ã¯ã2ã€ã®ãã¿ã³ãæŒããŠïŒMainPage_Loadedããã·ãŒãžã£ã§ïŒããŒãžã®ããŒã¿ã³ã³ããã¹ããèšå®ããåå¿ã«éå®ãããŸãã UserManagerã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ããäž»èŠãªäœæ¥ãæ¢ã«è¡ãããŠããããŒã¿ã³ã³ããã¹ããšããŠæ©èœããããšãæšæž¬ããããšã¯é£ãããããŸããã ãã®ã¯ã©ã¹ã¯ãããŒãžã¬ã€ã¢ãŠããããã£ãŒã«ãã«ãã€ã³ãã§ããINotifyPropertyChangedã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããŸãã
移æ€ãããã³ãŒãã¯ããã€ãã®å€æŽãåãåããå ã®ãµã³ãã«ãäœæãããã®ãšåãé åºã§ãããã解æããããšããŸãã
- ConnectUserããã·ãŒãžã£ã åºæ¬çã«ãããã°ã€ã³ããã¿ã³ãã¯ãªãã¯ããŠä»¥åã«åŒã³åºãããã³ãŒããè€è£œããŸãã ãã®ããã·ãŒãžã£ãžã®åŒã³åºãã¯ãããã¯ã°ã©ãŠã³ãæ¥ç¶çšã®ããŒã¯ã³ããªãå Žåã«ã®ã¿å¿ èŠã§ãã
- BuildPaneAsyncã¯ãããã€ãã®éèŠãªå€æŽãåãåããŸããã éå§ããã«ã¯ãAccountCommandsRequestedã€ãã³ããããµãã¹ã¯ã©ã€ãã解é€ããŸãããã®ã€ãã³ãã¯ããã®ããã·ãŒãžã£ãåŒã³åºããåŸã«äžèŠã«ãªããŸãã ããã«ã¯ãæ¢ç¥ã®ã³ãŒããç¶ããŸããããã¯ããããŒãžã£ãŒãŠã£ã³ããŠã§å°èŠåºããèšå®ãããã©ã€ãã·ãŒããªã·ãŒãžã®ãªã³ã¯ãè¿œå ããããšã§è£å®ãããŸãã ãããã®ãã€ã³ãã¯äž¡æ¹ãšããªãã·ã§ã³ã§ãããããªãã®è£éã§å®å
šã«å®è£
ãããŸãã ããã«ã2çªç®ã®ã±ãŒã¹ã§ã¯ãä»»æã®ã³ãŒããå®è¡ãããªã³ã¯ãè¿œå ã§ããŸãããã©ã€ãã·ãŒããªã·ãŒã®ããããŒãžã«ç§»åããå¿
èŠã¯ãããŸããã
- [ç¶è¡]ãã¿ã³ãã¯ãªãã¯ãããšãGetMsaTokenAsyncããã·ãŒãžã£ãéå§ãããã®ã§ãæåã®è¡ã§ãã°ãªã³ã¹ããŒã¿ã¹ãèšå®ããŸãããã®ã¹ããŒã¿ã¹ã«å¿çããŠãé²è¡ç¶æ³ã€ã³ãžã±ãŒã¿ã®è¡šç€ºãŸãã¯ã€ã³ã¿ãŒãã§ã€ã¹èŠçŽ ã®ãããã¯ãè¡ããŸãã 次ã«ããã§ã«ããã£ãŠããã³ãŒãããããŸãããå ¥åããã£ã³ã»ã«ãŸãã¯å€±æããç¶æ³ãžã®åå¿ãè¿œå ãããŠããŸãã ç§ã¯èªããŠãããç§ã¯ãã®äžèŠæ®éãªå Žæã®ããã«äžæ¥äžæŠã£ãã ãã£ããã¯ãå ¥åãªãã·ã§ã³ãéžæãããŠã£ã³ããŠã®çŽåŸã«ãã®ã³ãŒããåããŠå®è¡ãããšãããŒã¿ã«ã¢ã¯ã»ã¹ããããã®èš±å¯ãæ±ããå¥ã®ãŠã£ã³ããŠã衚瀺ãããããšã§ãã ããã2ã€ã®ãŠã£ã³ããŠã®åãæ¿ãã®éã«æ¯èŒçé·ãæéïŒæ倧2ç§ïŒãçµéããå Žåããããããããã°ã€ã³ããã¿ã³ãç¹°ãè¿ãæŒãããšãæé€ããããã«ããã®æéäžã¯ãã°ãªã³ç¶æ ãç¶æããã®ãçã«ããªã£ãŠããŸãã ãã ãããŠãŒã¶ãŒã¯ããŒã¿ãžã®ã¢ã¯ã»ã¹ã®æäŸãæåŠãããã2çªç®ã®ãŠã£ã³ããŠãéãããããã ãã§ãïŒã¢ãã€ã«ããã€ã¹ã®[æ»ã]ãã¿ã³ãã¯ãªãã¯ããŸãïŒããã®ããããã®ãããªã¢ã¯ã·ã§ã³ã®å Žåãå ã®ã¹ããŒã¿ã¹ãNoneã«æ»ãå¿ èŠããããŸããããã¯Elseãããã¯ã§è¡ããŸãã
- æ°ããããªãšãŒã·ã§ã³ã®BackgroundConnectUserããã·ãŒãžã£ã¯éåæé¢æ°ã«ãªã£ããããããã¯ã°ã©ãŠã³ãã§ã®ããŒã¿åä¿¡ã®å€±æïŒãŸãã¯æåïŒã®è©Šè¡ã«å¿ããŠå¯Ÿå¿ã§ããŸãã ãã®é¢æ°ã¯ããŸããŸãªå ŽæïŒã¹ãã©ãã·ã¥ã¹ã¯ãªãŒã³ãªã©ïŒããåŒã³åºãããšãã§ãããããããŒããããã¢ã¯ã»ã¹ããŒã¯ã³ã空ã§ãªãããšã確èªããå¿ èŠããããŸãã ä¿åãããããŒã«ãŒãèªã¿èŸŒãããã«ãå¥ã®GetWebAccounté¢æ°ãäœæãããŠããŸããããŒã«ã«èšå®ã«ããŒã«ãŒããªãå Žåã¯ãNothingãè¿ããŸãã WebAccountãªããžã§ã¯ãã®ååŸã«å€±æããå Žåã¯ããã©ã«ãã®çµæïŒFalseïŒãè¿ããæåããå Žåã¯ã¹ããŒã¿ã¹ãLogonã«èšå®ããŠæé ãç¶è¡ããŸãã 次ã«ãæ¢ç¥ã®ã¢ã¯ã·ã§ã³ãç¹°ãè¿ããŸãããå¯äžã®éãã¯ãããŒã¯ã³ã®åä¿¡ãæåŠããå Žåã¯Noneã¹ããŒã¿ã¹ãè¿ãå¿ èŠããããæåãããªã¯ãšã¹ãã§ã¯Ssuccessfulã§ãããšããããšã§ãã
- LogOutUserããã·ãŒãžã£ã«ã¯ã以åã¯äœ¿çšãããŠããªãã£ãã³ãŒããå«ãŸããŠããŸãã ã¢ã«ãŠã³ããçµäºããä¿åãããèšå®ããããŒã«ãŒãåé€ããæ©èœãå®è£ ããŸãã ãã®æ©äŒãã¢ããªã±ãŒã·ã§ã³ã«å®è£ ããŠãã ãããããã«ãããä»ã®è³æ Œæ å ±ãå ¥åããå¿ èŠæ§ã¯èšããŸã§ããªããæ©å¯æ§ã®ç¢ºä¿ã«é¢ããŠãŠãŒã¶ãŒã«å®å šãªèªç±ãäžããããŸãã
- ããŠãæåŸã«èšåããã®ã¯ããã®ã¯ã©ã¹ã§äœæããConnectStatusããããã£ã§ãã ãã®ããããã£ã«ã¯ãç¬èªã®åæåUserConnectStatusEnumã®ã¿ã€ããããããã°ã€ã³ã¹ããŒã¿ã¹ãèšå®ããããã«å¿ èŠã§ãã ããã¯ã¯ã©ã¹ãšã®å¯Ÿè©±ã®éèŠãªèŠçŽ ã§ãããã®ããããã£ã«ãã€ã³ãããããšã«ãããã³ãŒãã®ç¹å®ã®ã»ã¯ã·ã§ã³ã®å®è¡æã«æŒãã®ãæãŸãããªããã¿ã³ããããã¯ããããéã«ãã°ã€ã³åŸã«ã®ã¿äœ¿çšå¯èœãªãã¿ã³ãããã¯è§£é€ã§ããããã§ãã IsEnabledããããã£ã®ConnectStatusãžã®ãã€ã³ããæ£ãã解éããããã«ãConnectStatusToEnabledConverterã³ã³ããŒã¿ãŒãäœæãããŸããã
- . - - , , , , , .
, , . .