Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Oauth重定向Uri返回时,UWP OnNavigatedTo未触发_C#_Uwp_Oauth_Google Authentication - Fatal编程技术网

C# 从Oauth重定向Uri返回时,UWP OnNavigatedTo未触发

C# 从Oauth重定向Uri返回时,UWP OnNavigatedTo未触发,c#,uwp,oauth,google-authentication,C#,Uwp,Oauth,Google Authentication,首先,我在这里获取了Google提供的UWP应用程序示例() 我已经成功地使用了我自己的clientID(这里的答案中记录了该设置:)。重定向回应用程序工作正常,并从示例中的OnNavigatedTo()方法触发 然后我有一个UWP记录传感器的温度(最终将在Raspberry Pi上运行)——但现在我在正常的Windows 10上运行这个。我已经将Oauth位从示例复制到我的项目中,并且还使用名称“pw.oauth2”在Package.appxmanifest中声明了协议(与示例相同,也与我在G

首先,我在这里获取了Google提供的UWP应用程序示例()

我已经成功地使用了我自己的clientID(这里的答案中记录了该设置:)。重定向回应用程序工作正常,并从示例中的OnNavigatedTo()方法触发

然后我有一个UWP记录传感器的温度(最终将在Raspberry Pi上运行)——但现在我在正常的Windows 10上运行这个。我已经将Oauth位从示例复制到我的项目中,并且还使用名称“pw.oauth2”在Package.appxmanifest中声明了协议(与示例相同,也与我在Google开发者控制台中的声明相匹配)

当我启动应用程序并单击“使用谷歌登录”时,它会成功启动浏览器,请求授权/登录,并重定向回应用程序。我知道这部分工作正常,因为它将应用程序带回前台。但是,当返回到我的应用程序时,OnNavigatedTo()方法没有触发

我已经搜索并阅读了相关文档,但无法在我的应用程序中启动该方法。我觉得我遗漏了在示例应用中设置的、我尚未在应用中配置的其他内容。任何帮助或建议都将不胜感激

这是我的XAML:

<Page
    x:Class="CamIOT.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CamIOT"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <RelativePanel>
            <Button x:Name="button" HorizontalAlignment="Left" Margin="18,22,0,0" VerticalAlignment="Top" Width="206" Height="46" Click="oauth_Click"
                BorderThickness="0" Padding="0" Background="Transparent">
                <Image Source="Assets/btn_google_sign-in.png" Stretch="UniformToFill" HorizontalAlignment="Left" Width="206"/>
            </Button>

            <TextBox x:Name="textBoxOutput" TextWrapping="Wrap" Text=""
                IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="300,40,0,0" Height="731" Width="1000"/>
            <ScrollViewer Margin="12,100,12,12">
                <StackPanel>
                    <TextBlock TextWrapping="Wrap">
                 I2C Device Data
                    </TextBlock>
                    <TextBlock TextWrapping="Wrap" Margin="0,10,0,0">
                    Temperature and Humidity Data
                    </TextBlock>
                    <ScrollViewer HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Auto">
                        <Image Source="Assets/i2c_sample_bb.jpg" Stretch="None" Margin="2,10,2,0" />
                    </ScrollViewer>
                    <Button x:Name="StartStopButton" Content="Start" Margin="0,10,0,0" Click="{x:Bind StartStopScenario}"/>
                    <TextBlock x:Name="ScenarioControls" Visibility="Collapsed">
                    Current Temperature: <Run x:Name="CurrentTemp"/>
                    <LineBreak/>
                    Current Humidity: <Run x:Name="CurrentHumidity"/>
                    </TextBlock>
                </StackPanel>
            </ScrollViewer>
        </RelativePanel>
    </Grid>
</Page>

I2C设备数据
温湿度数据
当前温度:
当前湿度:
以下是我的OnNavigatedTo()方法:

受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
Debug.WriteLine(“onnavigatedtofired!”);
if(e.参数为Uri)
{
//从导航参数获取URI。
Uri authorizationResponse=(Uri)e.参数;
string queryString=authorizationResponse.Query;
输出(“主页面收到授权响应:+authorizationResponse”);
//将URI参数解析到字典中
//参考:http://stackoverflow.com/a/11957114/72176
字典查询字符串参数=
queryString.Substring(1).Split(“&”)
.ToDictionary(c=>c.Split('=')[0],
c=>Uri.UnescapeDataString(c.Split('=')[1]);
if(queryStringParams.ContainsKey(“错误”))
{
输出(String.Format(“OAuth授权错误:{0}.”,queryStringParams[“error”]);
返回;
}
如果(!queryStringParams.ContainsKey(“代码”)
||!queryStringParams.ContainsKey(“状态”))
{
输出(“格式错误的授权响应”。+queryString);
返回;
}
//获取授权代码和状态
字符串代码=queryStringParams[“代码”];
字符串传入_state=queryStringParams[“state”];
//从本地设置(发出请求时保存)检索预期的“状态”值。
ApplicationDataContainer localSettings=ApplicationData.Current.localSettings;
字符串应为_state=(字符串)localSettings.Values[“state”];
//将接收到的状态与预期值进行比较,以确保
//此应用发出了导致授权的请求
if(传入的\u状态!=预期的\u状态)
{
输出(String.Format(“接收到的请求具有无效状态({0})”,传入的_状态));
返回;
}
//重置预期状态值以避免重播攻击。
localSettings.Values[“state”]=null;
//授权码现在可以使用了!
输出(Environment.NewLine+“授权代码:”+代码);
字符串代码校验器=(字符串)本地设置。值[“代码校验器”];
performCodeExchangeAsync(代码,代码校验器);
}
其他的
{
Debug.WriteLine(e.Parameter);
}
}
有没有关于我遗漏了什么以及为什么OnNavigatedTo()在示例应用程序中可以启动的想法,但在我的应用程序中没有


提前谢谢你的帮助

最终我自己解决了这个问题-如果您在问题中的链接中遵循Google提供的UWP Oauth应用示例-您需要将下面的此方法添加到App.xaml.cs类中-当Google重定向打开您的应用程序时,将调用此方法。然后它会导航到主页面,在主页面上激发OnNavigatedTo()方法中的代码

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Debug.WriteLine("OnNavigatedTo Fired!");

            if (e.Parameter is Uri)
            {
                // Gets URI from navigation parameters.
                Uri authorizationResponse = (Uri)e.Parameter;
                string queryString = authorizationResponse.Query;
                output("MainPage received authorizationResponse: " + authorizationResponse);

                // Parses URI params into a dictionary
                // ref: http://stackoverflow.com/a/11957114/72176
                Dictionary<string, string> queryStringParams =
                        queryString.Substring(1).Split('&')
                             .ToDictionary(c => c.Split('=')[0],
                                           c => Uri.UnescapeDataString(c.Split('=')[1]));

                if (queryStringParams.ContainsKey("error"))
                {
                    output(String.Format("OAuth authorization error: {0}.", queryStringParams["error"]));
                    return;
                }

                if (!queryStringParams.ContainsKey("code")
                    || !queryStringParams.ContainsKey("state"))
                {
                    output("Malformed authorization response. " + queryString);
                    return;
                }

                // Gets the Authorization code & state
                string code = queryStringParams["code"];
                string incoming_state = queryStringParams["state"];

                // Retrieves the expected 'state' value from local settings (saved when the request was made).
                ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
                string expected_state = (String)localSettings.Values["state"];

                // Compares the receieved state to the expected value, to ensure that
                // this app made the request which resulted in authorization
                if (incoming_state != expected_state)
                {
                    output(String.Format("Received request with invalid state ({0})", incoming_state));
                    return;
                }

                // Resets expected state value to avoid a replay attack.
                localSettings.Values["state"] = null;

                // Authorization Code is now ready to use!
                output(Environment.NewLine + "Authorization code: " + code);

                string code_verifier = (String)localSettings.Values["code_verifier"];
                performCodeExchangeAsync(code, code_verifier);
            }
            else
            {
                Debug.WriteLine(e.Parameter);
            }
        }
 protected override void OnActivated(IActivatedEventArgs args)
        {
            // When the app was activated by a Protocol (custom URI scheme), forwards
            // the URI to the MainPage through a Navigate event.
            if (args.Kind == ActivationKind.Protocol)
            {
                // Extracts the authorization response URI from the arguments.
                ProtocolActivatedEventArgs protocolArgs = (ProtocolActivatedEventArgs)args;
                Uri uri = protocolArgs.Uri;
                Debug.WriteLine("Authorization Response: " + uri.AbsoluteUri);

                // Gets the current frame, making one if needed.
                var frame = Window.Current.Content as Frame;
                if (frame == null)
                    frame = new Frame();

                // Opens the URI for "navigation" (handling) on the MainPage.
                frame.Navigate(typeof(MainPage), uri);
                Window.Current.Content = frame;
                Window.Current.Activate();
            }
        }