Skipping the login screen in XAF

Here is a problem I had recently.

I have a XAF application that is called from another application. The
users have already logged into the other application, so I don’t want
them to have to login to my application separately. I also don’t want
them to have to enter the same users over again. Here is a solution to
the problem in XAF. The name of the user is passed on the command line
from other application. If the user does not exist then I automatically
create him and set his role to "Users".

First create a class to handle authentication.

using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using System.ComponentModel;
using DevExpress.Persistent.Base;
using DevExpress.Data.Filtering;

namespace SOSRCKCAssessment.Module
{
 
    public class SOSAuthentication : AuthenticationBase
    {
        public SOSAuthentication(string who) : base()
        {
            Who = who;
        }

        public string Who;
     
        public override bool AskLogonParametersViaUI
        {
            get { return false; }
        }

        public override bool IsLogoffEnabled
        {
            get { return false; }
        }

        public override object Authenticate(DevExpress.ExpressApp.ObjectSpace objectSpace)
        {
            User user = objectSpace.FindObject<User>(new BinaryOperator("UserName", Who));
            if (user == null)
            {
                user = new User(objectSpace.Session);
                user.UserName = Who;
                user.FirstName = Who;
                Role role = objectSpace.FindObject<Role>(new BinaryOperator("Name", "Users"));
                if (role != null)
                    user.Roles.Add(role);
                user.Save();
                objectSpace.CommitChanges();
            }
            return user;
        }
    }
}

Notice
that this class inherits from AuthenticationBase supplied by
DevExpress. It overrides AskLogonParametersViaUI and returns false so
that XAF does not try and show a login screen.
The name of the user (gotten from the command line) is stored in Who.

Next I modified my program file as follows:

using System;
using System.Configuration;
using System.Windows.Forms;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Win;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using SOSRCKCAssessment.Module;

namespace SOSRCKCAssessment.Win
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        ///
 

        [STAThread]
        static void Main(string[] args)
        {

            string user = args[0];
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            EditModelPermission.AlwaysGranted = System.Diagnostics.Debugger.IsAttached;
            SOSRCKCAssessmentWindowsFormsApplication application = new SOSRCKCAssessmentWindowsFormsApplication();
            if (ConfigurationManager.ConnectionStrings["ConnectionString"] != null)
            {
                application.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            }            
            SOSAuthentication loginUser = new SOSAuthentication(user);
            application.Security = new SecurityComplex<User, Role>(loginUser);
            try
            {
                application.Setup();
                application.Start();
            }
            catch (Exception e)
            {
                application.HandleException(e);
            }
        }

    }
}

here
I create my SOSAuthentication class passing in the user name from args
(the command line) and set application.Security to it. That is it.
Quite simple. My changes to the standard program file are shown in red.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment