Sunday, September 25, 2011

How to change the customer / partner portal to authenticate against the MSCRM only.


How to change the customer / partner portal to authenticate against the MSCRM only.


There is a new version for the portals, to see how to convert Customer Portal V2 go to http://dynamicslollipops.blogspot.co.il/2012/10/how-to-change-customer-partner-portal.html


This blog is based on few other blogs:
Active Directory Authentication:
http://www.shanmcarthur.net/crm/developers-corner/customer-portal-modifications-for-demo-vpc-without-internet-connectivity
SqlMembershipProvider: http://intergr8it.net/?p=216
Both are great and doing the work.
Yet I had to create another approach.

As you probably know the portals comes with authentication against LiveID, Even that it's best practice and all, sometimes the portal isn't expost to the internet and simpler authentication is needed.

Basically the authentication options are:

1)      LiveID – Each user has to have account in Microsoft LiveId website in order to use the portal.

2)      Active Directory – Each user has to be active directory user (almost same as every MSCRM user)

3)      SqlMemberhipProvider – Each user will be created in separate sql table.

4)      Form authentication – It's up to you (In code) who to authenticate and who not. This is the one we'll use to authenticate user based only on the data in the MSCRM.

In this Solution register page is droped and the crm administrator is registers the users stright in the crm. Once the user is configured in the crm his good to go.

Walkthrough:


In order to do it you'll need to download the portal and follow the installation steps, avoid all the stuff regarding the LiveID.

After you've imported the solution and the website data we are ready to go.

1)      Open the website in visual studio.

2)      Edit he web.config.

a.       Remove the
<add name="Live" connectionString="Application Id=0000000000000000; Secret=00000000000000000000000000000000"/>

From the connectionstring tag

b.      Remove the
<membership defaultProvider="CrmMembershipProvider">
<providers><add name="CrmMembershipProvider" type="Microsoft.Xrm.Portal.Web.Security.LiveIdMembershipProvider, Microsoft.Xrm.Portal" liveIdConnectionStringName="Live"/></providers></membership>

c.        Replace the Authentication tag to as follow
<authentication mode="Forms">
<forms loginUrl="/login" timeout="525600"     defaultUrl="/"  />

 </authentication>

3)      Edit \MasterPages\Default.master.

a.       Replace

<crm:LiveIdLoginStatus ID="LiveLoginStatus" runat="server" LoginNavigateUrl='<%$ CrmSiteMap: SiteMarker=Login, Eval=Url %>'/>

With

<asp:LoginStatus ID="LoginStatus1" runat="server" />

4)      Edit \Pages\Login.aspx

a.        Replace               
<crm:LiveIdLoginStatus ID="LiveIdLink" runat="server" LoginImageUrl="http://www.passportimages.com/1033/signin.gif"

LogoutImageUrl="http://www.passportimages.com/1033/signout.gif" />

With

 <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate"></asp:Login>

5)       Edit \Pages\Login.aspx.cs

a.        Add:
  private Contact _loginContact;



        protected Contact LoginContact

        {

            get

            {

                return _loginContact ??

                (_loginContact = XrmContext.ContactSet

                .FirstOrDefault(c => c.Adx_username == Login1.UserName && c.Adx_LogonEnabled != null && c.Adx_LogonEnabled.Value));

            }

        }

b.        Replace onload:
protected void Page_Load(object sender, EventArgs e)

        {

            if ((User != null) && User.Identity.IsAuthenticated)

            {

                var redirectUrl = !string.IsNullOrEmpty(Request.QueryString["ReturnUrl"])

               ? Request["ReturnUrl"]

               : !string.IsNullOrEmpty(Request.QueryString["URL"])

                   ? Request["URL"]

                   : "/";



                Response.Redirect(redirectUrl);

            }

        }

c.        Add:
  protected void Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)

        {

            if (LoginContact == null)

            {

                e.Authenticated = false;

            }

            else

            {

                if (LoginContact.Adx_password == Login1.Password)

                {

                    if (LoginContact.Adx_changepasswordatnextlogon.Value)

                    {

                        var page = ServiceContext.GetPageBySiteMarkerName(Website, "ChangePassword");

                        string redirectURL = ServiceContext.GetUrl(page) + "?UserName=" + Server.UrlEncode(Login1.UserName) + "&Password=" + Server.UrlEncode(Login1.Password);

                        Response.Redirect(redirectURL);

                    }

                    else

                    {

                        LoginContact.Adx_LastSuccessfulLogon = DateTime.Now.Date;



                        XrmContext.UpdateObject(LoginContact);

                        XrmContext.SaveChanges();



                        e.Authenticated = true;

                       // Response.Redirect("/");

                        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);

                    }

                }

                else

                {

                    e.Authenticated = false;

                }

            }

        }



Compile, Debug and Publish to the IIS.

13 comments:

  1. Just FYI. I believe you're messing a couple of entries that have to be added to the Login.aspx.cs page...

    using Xrm;
    using System.Web.Security;
    using System.Linq;

    ReplyDelete
  2. I guess i've added them without noticing it.
    Thank you.

    ReplyDelete
  3. hi yairrose.
    I used u r code the website is opening but when i going login it does not supporting contact credentials giving your login attempt failed try again.can u resolve the error.

    ReplyDelete
    Replies
    1. Hi,
      Did you log on enabled the contact?
      Go to Contact form->Web Authentication (Tab)-> Logon Enabled should be checked. Otherwise the user can't access the portal.
      If that doesn't help I'll need more information from you.

      Delete
  4. good morning

    these instructions are for crm OnPremise or online?

    ReplyDelete
  5. Hi,
    It doesn't matter, It's for all environments.
    The change is only for the authentication part.

    ReplyDelete
  6. I'm confused here. I can't find crm:LiveIdLoginStatus ID="LiveLoginStatus" runat="server" LoginNavigateUrl='<%$ CrmSiteMap: SiteMarker=Login, Eval=Url %>'/ in the Default.master page to replace it. Also with the login.aspx.cs file. Xrm.XrmServiceContext doesn't have a definition for GetPageBySiteMarkerName or GetUrl. Am I missing a reference here??

    ReplyDelete
    Replies
    1. Hi,
      Thanks for bringing it to my notice.
      Microsoft released lately newer version of the (Customer) portal with code differences.
      Unfortunately i don't have the time to update the blog, but I promise to do it.

      Delete
  7. Hi yairrose,

    what about the update? Have you already started?

    ReplyDelete
  8. Hi, has anyone found the instructions for doing (above) with the new version of the Portal? Cheers!

    ReplyDelete
    Replies
    1. Yah I'll post it soon. Hope i'll have time to it in the weekend.

      Delete
    2. That would be very appreciated yairrose. I'll check again on Monday. Thx

      Delete
    3. I've posted the solution, You have asked for.

      Delete