質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

1回答

1337閲覧

「Visual Studio 2019」にて、「FWP」のC#プロジエクトで「Basic認証」をする方法

kishidamisao

総合スコア12

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2021/05/28 04:57

「Visual Studio 2019」にて、FWPのC#でプロジエクトを作成しました。
「webView.CoreWebView2.Navigate(URL)を使用して「Basic認証」を
行いたいのですが方法がわかりません。

UWPでは、「HttpBaseProtocolFilter()」クラスを使用していたので
認証していたのですが、FWPプロジェクトで、「HttpBaseProtocolFilter()」クラス
を使用する方法が分かりません。
または、FWPプロジェクトを使用して「Basic認証」をする方法をご教示をお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kishidamisao

2021/05/28 05:51

すみません、私が間違ってました。FPWです。
kishidamisao

2021/05/28 05:58

UWPでは、以下のように行っていたのですが、WPFでは、どのようにすればよいのでしょうか? var myFilter = new HttpBaseProtocolFilter(); myFilter.ServerCredential = new PasswordCredential(hagaa, user_name, spasswward); //var filter = new HttpBaseProtocolFilter(); //filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential(Url, user_name, spasswward); //Windows.Web.Http.HttpClient client1 = new Windows.Web.Http.HttpClient(filter); var response = await client1.GetAsync(new Uri(Url)); WebClient myweb = new WebClient(); myweb.Credentials = new NetworkCredential("User", "PassWord");  HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse webres = null;
dodox86

2021/05/28 07:20

@質問者kishidamisaoさん > すみません、私が間違ってました。FPWです。 とりあえず、質問件名や説明文中の誤字を「WPF」に直しましょう。質問は編集、追記できます。 また、コードを質問文中に提示するときはマークダウン記法が使って読みやすくしてください。 https://teratail.com/help/question-tips#questionTips3-5-1 https://teratail.com/questions/238564 などを参考にしてください。 HttpBaseProtocolFilterもPasswordCredentialもUWPでしか使えないので、 「C# WPF HTTP Basic認証」などとキーワード検索して調べ、コードを書き直した方が早いでしょう。
guest

回答1

0

FWPでもFPWでもWFPでもなくWPFです!(Windows Presentation Foundation)

CoreWebView2ということはWebView2ということでしょうかね。

普通にurlに直接書いても通りました(https://user:pass@
ちゃんと?やるならこんな感じのようです(コメントを外してください)

xml

1<Window 2 x:Class="Questions340844.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" 6 Width="800" 7 Height="450"> 8 <DockPanel> 9 <DockPanel DockPanel.Dock="Top"> 10 <Button 11 Click="ButtonGo_Click" 12 Content="Go" 13 DockPanel.Dock="Right" /> 14 <TextBox Name="addressBar" /> 15 </DockPanel> 16 <wv2:WebView2 Name="webView" Source="https://www.microsoft.com" /> 17 </DockPanel> 18</Window>

cs

1using Microsoft.Web.WebView2.Core; 2using System; 3using System.Text; 4using System.Windows; 5 6namespace Questions340844 7{ 8 public partial class MainWindow : Window 9 { 10 public MainWindow() 11 { 12 InitializeComponent(); 13 addressBar.Text = "https://user:pass@httpbin.org/basic-auth/user/pass"; 14 15 //InitializeAsync(); 16 } 17 private async void InitializeAsync() 18 { 19 addressBar.Text = "https://httpbin.org/basic-auth/user/pass"; 20 21 await webView.EnsureCoreWebView2Async(); 22 webView.CoreWebView2.AddWebResourceRequestedFilter("https://httpbin.org/basic-auth/*", CoreWebView2WebResourceContext.All); 23 webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested; 24 } 25 26 private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e) 27 { 28 var authData = Encoding.UTF8.GetBytes("user:pass"); 29 var authHeader = $"Basic {Convert.ToBase64String(authData)}"; 30 e.Request.Headers.SetHeader("Authorization", authHeader); 31 } 32 33 private void ButtonGo_Click(object sender, RoutedEventArgs e) 34 { 35 if (webView != null && webView.CoreWebView2 != null) 36 { 37 webView.CoreWebView2.Navigate(addressBar.Text); 38 } 39 } 40 } 41}

c# - Edit HTTP Request header with WebView2 - Stack Overflow

投稿2021/05/28 09:00

編集2023/07/27 14:26
TN8001

総合スコア9317

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kishidamisao

2021/06/07 09:00

ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問