none
关于CRMService的权限的问题? RRS feed

  • 问题

  • 我的代码是这样写的
     public static CrmService CreateCrmService()
    
      {
    
       CrmAuthenticationToken token = new CrmAuthenticationToken();
    
       token.AuthenticationType = 0;
    
       token.OrganizationName = "SZYQ";
    
    
    
       CrmService service = new CrmService();
    
       service.CrmAuthenticationTokenValue = token;
    
    
    
    
    
       service.Credentials = new NetworkCredential(userName, password, domain);
    
       //service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    
    
       
    
       return service;
    
    
    
      }
    
    

    这样写完后,用户名字和密码是写死的,部署到IIS上可以正常调用CRMWebservice,可是当我相得到 当前用户名的时候,我用注释的代码替换掉它上面的代码啦,在vs中调试的时候也能根据当前的用户调用CRMWebservice可是当部署到IIS上后就不能正常调用啦,出现不能调用CRMWebservice的401错误,我不知道怎么样把它改成我根据当前用户调用CRMService.应该怎么配置IIS,请高手指导我一下,非常感谢

    出现的异常如下所示  

    请求因 HTTP 状态 401 失败: Unauthorized。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
    
    异常详细信息: System.Net.WebException: 请求因 HTTP 状态 401 失败: Unauthorized。
    
    源错误: 
    
    
    行 311:    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple", RequestNamespace="http://schemas.microsoft.com/crm/2007/WebServices", ResponseNamespace="http://schemas.microsoft.com/crm/2007/WebServices", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    行 312:    public BusinessEntityCollection RetrieveMultiple(QueryBase query) {
    行 313:      object[] results = this.Invoke("RetrieveMultiple", new object[] {
    行 314:            query});
    行 315:      return ((BusinessEntityCollection)(results[0]));
     
    
    源文件: D:\hongfu\Visual Studio 2008\Projects\McrosoftDynamics4.0\McrosoftDynamics4.0\CrmServer\CrmSdk\CrmSdk\Reference.cs  行: 313 

    CRM----BEST
    • 已编辑 王红福 2010年5月18日 2:27 问题描述不太清楚
    2010年5月17日 13:29

答案

  • Authentication from an ASPX Page
    crmsdk:
    using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.Crm.Sdk; using Microsoft.Crm.SdkTypeProxy; using Microsoft.Win32; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { public string orgname; public string crmurl; public string metaurl; public bool offline; protected void Page_Load(object sender, EventArgs e) { #region CRM URLs and Organization Name //Determine Offline State from Host Name Response.Write(Request.Url.Host.ToString()); if (Request.Url.Host.ToString() == "127.0.0.1") { offline = true; //Retrieve the Port and OrgName from the Registry RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\MSCRMClient"); orgname = regkey.GetValue("ClientAuthOrganizationName").ToString(); string portnumber = regkey.GetValue("CassiniPort").ToString(); //Construct the URLs string baseurl = "http://localhost:" + portnumber + "/mscrmservices/2007/"; crmurl = baseurl + "crmservice.asmx"; metaurl = baseurl + "metadataservice.asmx"; } else { offline = false; //Retrieve the URLs from the Registry RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM"); string ServerUrl = regkey.GetValue("ServerUrl").ToString(); crmurl = ServerUrl + "/2007/crmservice.asmx"; metaurl = ServerUrl + "/2007/metadataservice.asmx"; //Retrieve the Query String from the current URL if (Request.QueryString["orgname"] == null) { orgname = string.Empty; } else { //Query String string orgquerystring = Request.QueryString["orgname"].ToString(); if (string.IsNullOrEmpty(orgquerystring)) { orgname = string.Empty; } else { orgname = orgquerystring; } } if (string.IsNullOrEmpty(orgname)) { //Windows Auth URL if (Request.Url.Segments[2].TrimEnd('/').ToLower() == "isv") { orgname = Request.Url.Segments[1].TrimEnd('/').ToLower(); } //IFD URL if (string.IsNullOrEmpty(orgname)) { string url = Request.Url.ToString().ToLower(); int start = url.IndexOf("://") + 3; orgname = url.Substring(start, url.IndexOf(".") - start); } } } #endregion using (new CrmImpersonator()) { CrmAuthenticationToken token; if (offline == true) { token = new CrmAuthenticationToken(); } else { // Notice that the Context parameter value is Page.Context. token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname); } token.OrganizationName = orgname; token.AuthenticationType = 0; //Create the Service CrmService service = new CrmService(); service.Credentials = System.Net.CredentialCache.DefaultCredentials; service.CrmAuthenticationTokenValue = token; service.Url = crmurl; // This code shows how to create the metadata service. // It is not used in this sample. // MetadataService meta = new MetadataService(); // meta.CrmAuthenticationTokenValue = token; // meta.Credentials = CredentialCache.DefaultCredentials; // meta.Url = "http://localhost/mscrmservices/2007/MetadataService.asmx"; account account = new account(); account.name = "Offline Impersonator : " + DateTime.Now.TimeOfDay.ToString(); if (offline == false) // Explicitly set the owner ID for the record if not offline. account.ownerid = new Owner("systemuser", token.CallerId); service.Create(account); } Response.Write("Done"); } } }
    • 已标记为答案 王红福 2010年5月18日 5:48
    2010年5月18日 5:07

全部回复

  • Authentication from an ASPX Page
    crmsdk:
    using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.Crm.Sdk; using Microsoft.Crm.SdkTypeProxy; using Microsoft.Win32; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { public string orgname; public string crmurl; public string metaurl; public bool offline; protected void Page_Load(object sender, EventArgs e) { #region CRM URLs and Organization Name //Determine Offline State from Host Name Response.Write(Request.Url.Host.ToString()); if (Request.Url.Host.ToString() == "127.0.0.1") { offline = true; //Retrieve the Port and OrgName from the Registry RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\MSCRMClient"); orgname = regkey.GetValue("ClientAuthOrganizationName").ToString(); string portnumber = regkey.GetValue("CassiniPort").ToString(); //Construct the URLs string baseurl = "http://localhost:" + portnumber + "/mscrmservices/2007/"; crmurl = baseurl + "crmservice.asmx"; metaurl = baseurl + "metadataservice.asmx"; } else { offline = false; //Retrieve the URLs from the Registry RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM"); string ServerUrl = regkey.GetValue("ServerUrl").ToString(); crmurl = ServerUrl + "/2007/crmservice.asmx"; metaurl = ServerUrl + "/2007/metadataservice.asmx"; //Retrieve the Query String from the current URL if (Request.QueryString["orgname"] == null) { orgname = string.Empty; } else { //Query String string orgquerystring = Request.QueryString["orgname"].ToString(); if (string.IsNullOrEmpty(orgquerystring)) { orgname = string.Empty; } else { orgname = orgquerystring; } } if (string.IsNullOrEmpty(orgname)) { //Windows Auth URL if (Request.Url.Segments[2].TrimEnd('/').ToLower() == "isv") { orgname = Request.Url.Segments[1].TrimEnd('/').ToLower(); } //IFD URL if (string.IsNullOrEmpty(orgname)) { string url = Request.Url.ToString().ToLower(); int start = url.IndexOf("://") + 3; orgname = url.Substring(start, url.IndexOf(".") - start); } } } #endregion using (new CrmImpersonator()) { CrmAuthenticationToken token; if (offline == true) { token = new CrmAuthenticationToken(); } else { // Notice that the Context parameter value is Page.Context. token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname); } token.OrganizationName = orgname; token.AuthenticationType = 0; //Create the Service CrmService service = new CrmService(); service.Credentials = System.Net.CredentialCache.DefaultCredentials; service.CrmAuthenticationTokenValue = token; service.Url = crmurl; // This code shows how to create the metadata service. // It is not used in this sample. // MetadataService meta = new MetadataService(); // meta.CrmAuthenticationTokenValue = token; // meta.Credentials = CredentialCache.DefaultCredentials; // meta.Url = "http://localhost/mscrmservices/2007/MetadataService.asmx"; account account = new account(); account.name = "Offline Impersonator : " + DateTime.Now.TimeOfDay.ToString(); if (offline == false) // Explicitly set the owner ID for the record if not offline. account.ownerid = new Owner("systemuser", token.CallerId); service.Create(account); } Response.Write("Done"); } } }
    • 已标记为答案 王红福 2010年5月18日 5:48
    2010年5月18日 5:07
  • CrmService service = new CrmService();
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;
            service.CrmAuthenticationTokenValue = token;
            service.Url = crmurl;
    你这的引用是CemService的吧,那样引用不会有冲突吗


    CRM----BEST
    2010年5月18日 6:15
  • 不会有冲突
    2010年5月18日 12:48