domingo, 15 de septiembre de 2013

CRM 4.0 - Impersonación en páginas aspx

protected void Page_Load(object sender, EventArgs e)
    {
        string orgname;
        try {
            //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;
                }
            }

        //---------------------------------------

            using (new CrmImpersonator())
        {
            CrmAuthenticationToken token;
          
            token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname);
           
            token.OrganizationName = orgname;
            token.AuthenticationType = 0;
            //Create the Service
            CrmService Crmservice = new CrmService();
            Crmservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
            Crmservice.CrmAuthenticationTokenValue = token;
            string crmurl = @"http://127.0.0.1:5601/mscrmservices";
            Crmservice.Url = crmurl + "/2007/CrmService.asmx";
            account account = new account();
            account.name = "Offline Impersonator: " + DateTime.Now.TimeOfDay.ToString();
            systemuser usuario = new systemuser();
            usuario = (systemuser)Crmservice.Retrieve(EntityName.systemuser.ToString(), new Guid(Context.User.Identity.Name), new Query.AllColumns());
            if (usuario.domainname != null)
            {
                Response.Write(usuario.domainname);
                nombreusuario = usuario.domainname;
            }
           
        }

            //---------------------------------------

           
        }
        catch (SoapException sx) {
            Response.Write(sx.Detail.OuterXml);
        }

    }

No hay comentarios:

Publicar un comentario