【问题标题】:How to access secure soap web service basic authentication in Windows Phone 7如何在 Windows Phone 7 中访问安全的肥皂网络服务基本身份验证
【发布时间】:2012-05-09 08:14:15
【问题描述】:
  1. 我创建了一个 Web 服务 "http://service.mydomain.com/MobileService.asmx"
  2. 我创建了一个 Windows 移动应用程序
  3. 我添加了对此 Web 服务的引用
  4. 我开始编码并完成了 WP7 应用程序并部署了 we 服务
  5. 然后我使用 IIS 7.5 禁用匿名身份验证并使用基本身份验证来保护我的 Web 服务。
  6. 使用基本身份验证后,我添加了对我的服务的新引用,VS 2010 询问身份验证,我使用了我的用户名和密码
  7. 然后,当我尝试使用新服务时,我遇到了异常。

    Reference.cs 错误:

    public System.IAsyncResult BeginGetArticle(MyWP7App.MyService.GetArticleRequest request, System.AsyncCallback callback, object asyncState) { 对象[] _args = 新对象[1]; _args[0] = 请求; System.IAsyncResult _result = base.BeginInvoke("GetArticle", _args, callback, asyncState); 返回_结果; }


CommunicationException:{“远程服务器返回错误:NotFound。”}
状态描述:未授权

at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Delegate.DynamicInvokeOne(Object[] args)
   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Delegate.DynamicInvokeOne(Object[] args)
   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
   at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
   at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
   at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
   at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)



这就是我尝试连接到我的网络服务的方式:
在 ParsingHelper.cs 中:

using System;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
    namespace MyWP7App
    {
    [XmlRoot("root")]
    public class Categories
    {
        [XmlArray("Categories")]
        [XmlArrayItem("Category")]
        public ObservableCollection<Category> Collection { get; set; }
    }
    }
    public class Category
    {
        [XmlAttribute("ID")]
        public int ID { get; set; }
        [XmlAttribute("SubCategories")]
        public int SubCategoriesCount { get; set; }
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlArray("SubCategories")]
        [XmlArrayItem("SubCategory")]
        public ObservableCollection<SubCategory> Collection { get; set; }
    }


在 MyPage.xaml.cs 中:

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Serialization;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

    namespace MyWP7App
    {
        public partial class CategoriesPage : PhoneApplicationPage
        {
        private ObservableCollection<Category> itemsSource;
        public ObservableCollection<Category> ItemsSource
        {
            get
            {
                return this.itemsSource;
            }
            set
            {
                this.itemsSource = value;
            }
        }

        private static MyService.MobileServiceSoapClient Service = null;


        public PanoramaMainPage()
        {
            InitializeComponent();
            if (null == ItemsSource)
                GetCategories();
            else
                imtListBox.ItemsSource = this.ItemsSource;
        }

        private void GetCategories()
        {
            Service = new MyService.MobileServiceSoapClient();

            // I tried to do the following when the service is secure, but I had the same error:
            // Service.ClientCredentials.UserName.UserName = "Username";
            // Service.ClientCredentials.UserName.Password = "Password";

            Service.GetCategoriesCompleted += new EventHandler<MyService.GetCategoriesCompletedEventArgs>(Service_GetCategoriesCompleted);
            Service.GetCategoriesAsync();
        }
        void Service_GetCategoriesCompleted(object sender, MyService.GetCategoriesCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null || e.Error != null)
                {
                    MessageBox.Show("There was an error downloading the XML-file!");
                }
                if (!e.Cancelled)
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Categories));
                    XDocument document = XDocument.Parse("<root>" + e.Result.ToString() + "</root>");
                    Categories arts = new Categories();
                    arts = (Categories)serializer.Deserialize(document.CreateReader());
                    this.ItemsSourceListBox = arts.Collection;
                    imtListBox.ItemsSource = this.Items1Source;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Service.Abort();
        }
    }


【问题讨论】:

    标签: web-services windows-phone-7 windows-phone basic-authentication


    【解决方案1】:

    看看这篇文章 http://msdn.microsoft.com/en-us/library/ff637320(VS.96).aspx

    这篇文章详细介绍了如何传递用户名/密码。这是页面中的sn-p

    using (OperationContextScope contextScope = new OperationContextScope(svc.InnerChannel))
    {
        byte[] bc = System.Text.Encoding.UTF8.GetBytes(@"username" + ":" + "password"); // the string passed after basic:
        HttpRequestMessageProperty httpProps = new HttpRequestMessageProperty();
        httpProps.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(bc);
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpProps;
        // call the service.
    }
    

    【讨论】:

    • 谢谢,我在“// 调用服务”之后添加以下内容:svc.GetArticlesByCategoryIDCompleted += new EventHandler&lt;MySRV.GetArticlesByCategoryIDCompletedEventArgs&gt;(Completed); svc.GetCategoriesAsync();。评论,但即使在调试时也没有任何反应我看不到 GetArticlesByCategoryIDCompleted 的任何结果
    • 谢谢@HermitDave,我做错了,现在可以正常工作了:)
    • 我很抱歉@HermitDave 但我发现我使用的是我的测试服务 URL 而不是安全的 URL,它工作正常,但是每当我尝试使用部署的安全服务时,我都会得到同样的错误..对不起,我认为您的解决方案正在运行,但它不是:(
    • 注意:我正在使用 SOAP 服务 (msdn.microsoft.com/en-us/library/…) 我创建了一个 ASP.NET 3.5 Web 服务,然后将解决方案升级到 4.0,我没有使用 WCF 服务,这就是我的原因我收到此错误“CommunicationException”?
    • 我不确定您需要做什么。在应用程序中心创建一个帖子(可能有帮助)并指定它适用于 http 但不适用于 https
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多