Windows.Web.Http.HttpClient + WEB API Windows身份验证

编程入门 行业动态 更新时间:2024-10-28 02:32:25
本文介绍了Windows.Web.Http.HttpClient + WEB API Windows身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Windows.Web.Http.HttpClient连接到我的WEB API.应用程序尚未提示输入用户名和密码,但是最近我通过将AuthorizeAttribute过滤器从Action移到Class级别来更改了WEB API.现在,我的Windows store 8.1应用程序提示您输入用户名和密码.请让我知道如何将HttpClient设置为不提示登录名和密码.any1可以建议我是否需要在我的httpcleint中添加标头

Im using Windows.Web.Http.HttpClient to connect to my WEB API. Application haven't prompted for userid and password, but recently i changed WEB API by moving AuthorizeAttribute filter from Action to Class level. Now my Windows store 8.1 application prompt for user id and password. Please let me know how to set HttpClient to not prompt the login and password. Can any1 suggest me do i need to add header to my httpcleint

使用(Windows.Web.Http.HttpClient httpClient =新的Windows.Web.Http.HttpClient()){//添加用户代理标头var headers = httpClient.DefaultRequestHeaders;

using (Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient()) { // Add a user-agent header var headers = httpClient.DefaultRequestHeaders;

//从用户检查标头值的安全方法是TryParseAdd方法//由于我们知道此标头是可以的,因此我们将ParseAdd与一起使用将抛出异常//值错误- msdn.microsoft/en-us/library/windows/apps/dn440594.aspx

// The safe way to check a header value from the user is the TryParseAdd method // Since we know this header is okay, we use ParseAdd with will throw an exception // with a bad value - msdn.microsoft/en-us/library/windows/apps/dn440594.aspx

headers.UserAgent.ParseAdd("ie"); headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); using (var response = await httpClient.GetAsync(new Uri(url)))

我看不到发送默认凭据的方法.

I dont see a way to send Default credentials.

推荐答案

使用 HttpBaseProtocolFilter.AllowUI 禁用UI对话框.试试这个:

Disable UI dialogs using HttpBaseProtocolFilter.AllowUI. Try this:

Windows.Web.Http.Filters.HttpBaseProtocolFilter filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter(); filter.AllowUI = false; HttpClient client = new HttpClient(filter); Uri uri = new Uri("localhost/?basic=1"); var response = await client.GetAsync(uri); System.Diagnostics.Debug.WriteLine(response);

您需要凭证吗?使用 HttpBaseProtocolFilter.ServerCredential .试试这个:

Do you need credentials? Use HttpBaseProtocolFilter.ServerCredential. Try this:

Uri uri = new Uri("localhost?ntlm=1"); Windows.Web.Http.Filters.HttpBaseProtocolFilter filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter(); filter.AllowUI = false; // Set credentials that will be sent to the server. filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential( uri.ToString(), "userName", "abracadabra"); HttpClient client = new HttpClient(filter); var response = await client.GetAsync(uri); System.Diagnostics.Debug.WriteLine(response);

您是否需要默认的Windows凭据(域凭据)?只需将企业身份验证功能添加到 Package.appxmanifest .

Do you need default Windows credentials (domain credentials)? Simply add the Enterprise Authentication capability to your Package.appxmanifest.

更多推荐

Windows.Web.Http.HttpClient + WEB API Windows身份验证

本文发布于:2023-10-07 01:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1468067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Http   Web   Windows   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!