您的位置:首页 > 产品设计 > UI/UE

如何获取当前登录的用户的GUID,进而获取用户的信息

2010-07-06 17:41 821 查看
刚开始找到的,以为用WhoAmI消息去获取,后来在SDK中仔细的搜索了一遍,终于找到了解决方法。二话不说,来代码先:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Win32;

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
{
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";

Response.Write("<script type='text/javascript'>alert('Token:" + token.CallerId + "');</script>");// 这个就是了
//token.CallerId 获取当前用户id
}

Response.Write("Done");
}
}



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Win32;

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
            {
                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);
            Response.Write("<mce:script type='text/javascript'><!--
alert('Token:" + token.CallerId + "');
// --></mce:script>");
            //token.CallerId 获取当前用户id
            service.Create(account);
        }

        Response.Write("Done");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: