Getting an "401 Unauthorized" exception while trying to create User Profile in sharepoint through ASP.Net application running on a different website

 

Add the following register tag in your aspx page

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Add the sharepoint "FormDigest" control in the page.

<SharePoint:FormDigest ID="fd" runat="server"/>

Add the following code to add the user profile into sharepoint group:

SPSite site = null;

ServerContext server = null;

UserProfileManager upm = null

site = new SPSite("<https://servername/ssp/admin>");

server = ServerContext.GetContext(site);

upm = new UserProfileManager(server, false);

string sUser = "<User1>";

string sDomain = "Domain1";

////create user sample

string sAccount = sDomain + "\\" + sUser;

if (!upm.UserExists(sAccount))

{

           UserProfile u = upm.CreateUserProfile(sAccount);

           Response.Write("User created successfully"

}