不能隐式转换类型"字符串" to" Windows.Security.Credentials.PasswordCredential"

时间:2016-11-11 23:32:06

标签: c# windows-iot-core-10

无法隐式转换类型"字符串" to" Windows.Security.Credentials.PasswordCredential"  经过大约4个小时的搜索,我无法弄清楚这个错误。我在RPI 3上使用Windows 10 IOT Core。我的程序非常基本,但我无法转换代码中的数据类型来解决错误。

using SDKTemplate;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Windows.Devices.WiFi;
using Windows.Networking.Connectivity;
using Windows.Security.Credentials;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at          http://go.microsoft.com/fwlink/?LinkId=234238

namespace WiFiConnect
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a          Frame.
/// </summary>
public sealed partial class WiFiConnect_Scenario : Page
{
    MainPage rootPage;
    private WiFiAdapter firstAdapter;
    public ObservableCollection<WiFiNetworkDisplay> ResultCollection
    {
        get;
        private set;
    }

    public WiFiConnect_Scenario()
    {
        this.InitializeComponent();
    }

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        ResultCollection = new ObservableCollection<WiFiNetworkDisplay>();
        rootPage = MainPage.Current;

        // RequestAccessAsync must have been called at least once by the app      before using the API
        // Calling it multiple times is fine but not necessary
        // RequestAccessAsync must be called from the UI thread
        var access = await WiFiAdapter.RequestAccessAsync();
        if (access != WiFiAccessStatus.Allowed)
        {
            rootPage.NotifyUser("Access denied", NotifyType.ErrorMessage);
        }
        else
        {
            DataContext = this;

            var result = await    Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDevice       Selector());
            if (result.Count >= 1)
            {
                firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id);

                var button = new Button();
                button.Content = string.Format("Scan Available Wifi     Networks");
                button.Click += Button_Click;
                Buttons.Children.Add(button);
            }
            else
            {
                rootPage.NotifyUser("No WiFi Adapters detected on this     machine.", NotifyType.ErrorMessage);
            }
        }
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await firstAdapter.ScanAsync();
        ConnectionBar.Visibility = Visibility.Collapsed;
        DisplayNetworkReport(firstAdapter.NetworkReport);
    }

    private void DisplayNetworkReport(WiFiNetworkReport report)
    {
        rootPage.NotifyUser(string.Format("Network Report Timestamp: {0}",     report.Timestamp), NotifyType.StatusMessage);

        ResultCollection.Clear();

        foreach (var network in report.AvailableNetworks)
        {
            ResultCollection.Add(new WiFiNetworkDisplay(network, firstAdapter));
        }
    }

    private void ResultsListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selectedNetwork = ResultsListView.SelectedItem as WiFiNetworkDisplay;
        if (selectedNetwork == null)
        {
            return;
        }

        // Show the connection bar
        ConnectionBar.Visibility = Visibility.Visible;

        // Only show the password box if needed
        if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Open80211 &&
                selectedNetwork.AvailableNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None)
        {
            NetworkKeyInfo.Visibility = Visibility.Collapsed;
        }
        else
        {
            NetworkKeyInfo.Visibility = Visibility.Visible;
        }
    }

    private async void ConnectButton_Click(object sender, RoutedEventArgs e)
    {
        var selectedNetwork = ResultsListView.SelectedItem as WiFiNetworkDisplay;
        if (selectedNetwork == null || firstAdapter == null)
        {
            rootPage.NotifyUser("Network not selcted", NotifyType.ErrorMessage);
            return;
        }
        WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Manual;
        if (IsAutomaticReconnection.IsChecked.HasValue && IsAutomaticReconnection.IsChecked == true)
        {
            reconnectionKind = WiFiReconnectionKind.Automatic;
        }

        WiFiConnectionResult result;
        if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == Windows.Networking.Connectivity.NetworkAuthenticationType.Open80211 &&
                selectedNetwork.AvailableNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None)
        {
            result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind);
        }
        else
        {

            FileStream file = new FileStream("final-wordlist.txt", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            sr.ReadLine();
            var textLines = File.ReadAllLines("final-wordlist.txt");
            foreach (var line in textLines)
            {
                string[] dataArray = line.Split(' ');




                foreach (var item in dataArray)
                {
                    PasswordCredential credential = line;
                    //string credential = line.ToString();



            result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential);
                }


            }
            // Only the password potion of the credential need to be supplied

        }

        if (result.ConnectionStatus == WiFiConnectionStatus.Success)
        {
            rootPage.NotifyUser(string.Format("Successfully connected to {0}.", selectedNetwork.Ssid), NotifyType.StatusMessage);

            // refresh the webpage
            webViewGrid.Visibility = Visibility.Visible;
            toggleBrowserButton.Content = "Hide Browser Control";
            refreshBrowserButton.Visibility = Visibility.Visible;

        }
        else
        {
            rootPage.NotifyUser(string.Format("Could not connect to {0}. Error: {1}", selectedNetwork.Ssid, result.ConnectionStatus), NotifyType.ErrorMessage);
        }

        // Since a connection attempt was made, update the connectivity level displayed for each
        foreach (var network in ResultCollection)
        {
            network.UpdateConnectivityLevel();
        }
    }

    private void Browser_Toggle_Click(object sender, RoutedEventArgs e)
    {
        if (webViewGrid.Visibility == Visibility.Visible)
        {
            webViewGrid.Visibility = Visibility.Collapsed;
            refreshBrowserButton.Visibility = Visibility.Collapsed;
            toggleBrowserButton.Content = "Show Browser Control";
        }
        else
        {
            webViewGrid.Visibility = Visibility.Visible;
            refreshBrowserButton.Visibility = Visibility.Visible;
            toggleBrowserButton.Content = "Hide Browser Control";
        }
    }
    private void Browser_Refresh(object sender, RoutedEventArgs e)
    {
        webView.Refresh();
    }
}
}

2 个答案:

答案 0 :(得分:0)

试试这个:

PasswordCredential credential = null;

if (!string.IsNullOrEmpty(line))
{
    credential = new PasswordCredential()
    {
        Password = line
    };
}

答案 1 :(得分:0)

这对我有用。问题是它需要多个var来填充它。

var credential = new PasswordCredential("Module", "Username", "Password");