Accessing Multiple Host Applications based on different Data Source Projects Using Single Sign On

This section describes a way to use MVC to access separate host applications based on different data source projects using Single Sign On authentication. This allows you to use a single set of credentials to access two different host applications.

Note: For more information, refer to Creating an MVC Web Application and Sample Applications.

Perform the following steps to create single sign on page for accessing the applications:

  1. Customize the MVC project by creating a view that will link to both of the data source projects. For example create the file, Index.cshtml, in the following directory, Views/<datasource> for each data source. The following code can be added to link the login page of application 1 and application 2.

    @{
        ViewBag.Title = "Index";
    }
    <h2><b>Sample Application on Multiple DataSource project in single Presentation Project</b></h2>
    <div>
    <ul>
    <li>@Html.ActionLink("<application name 1>", "Index", "<Data Source Name 1>")</li>
    <li>@Html.ActionLink("<application name 2>", "Index", "<Data Source Name 2>")</li>
    </ul>
        </div>
  2. Add a class in the Controllers folder, one for each data source, as an extension of the data source controller.

    The following sample displays the code that can be added to save and re-use the credentials for an MCP COBOL data source project:

    namespace MultipleDataSourceMVCApp.Controllers
    {
        public partial class ebankMCPCobolDataSourceController : ebankMCPCobolDataSourceControllerBase
        {
            // GET: CobolDataSource
            public override ActionResult Login(ebankMCPCobolDataSource.Client.Login model)
            {
                // user submitted login screen
                // save the usercode and password
                Session["usercode"] = model.Usercode;
                Session["password"] = model.Password;
    
                return base.Login(model);
            }
    
            public override ActionResult Login()
            {
                if (Session.IsNewSession || CurrentMessage == null)
                    return RedirectToAction("Index");
    
                // about to send Login screen to user,
                // if credetials exist, send log1n to host
                // and return the resposne instead
                if (Session["usercode"] != null)
                {
         ebankMCPCobolDataSource.Client.Login model = new   ebankMCPCobolDataSource.Client.Login();
                    model.Usercode = (string)Session["usercode"];
                    model.Password = (string)Session["password"];
    
                    return SendReceive(model);
                }
                else
                {
                    return base.Login();
                }
    
            }
    
            public override ActionResult RuntimeError()
            {
                if (Session["ebankMCPCobolDataSource"] == null)
                    return RedirectToAction("Index");
    
                if (IsSinglePageApp)
                    return RedirectToAction("RuntimeErrorAjax");
    
                Session.Abandon();
                return View(CurrentMessage);
            }
    
        }
    }
  3. For incorrect credentials, add code to the Controller extension, by overriding the RuntimeError class and creating a view using the following code:

    @model Unisys.Error
    
    @{
        ViewBag.Title = "Application Error";
    }
    
    @using (Html.BeginForm("RuntimeError", @ViewContext.RouteData.Values["controller"].ToString(),
     FormMethod.Post, new { id = "form0" }))
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            @Html.ValidationSummary(true)       
            <div class="form-group">
                @*@Html.LabelFor(m => m.WebErrorMessage, new { @class = "control-label col-md-2" })*@
                col-md-10">
    <p class="form-control-static">
     @Html.DisplayFor(m => m.WebErrorMessage)
    </p>
         </div>
    </div>
    <div class="row">
    <div class="col-md-4 col-md-offset-4">@Html.ActionLink("REDIRECT TO LOGIN PAGE", "Index", "ebankMCPCobolDataSource")</div>
    </div>
    </div>
    }            
  4. Customize the MVC project Layout for all the pages using Layout.cshtml page. For example, add the following snippet of code in Layout.cshtml page in the directory Views/Shared for creating a panel that contains both application1 and application2.

    <div class="container body-content" style="padding-top:15px">
         <div class="row">
             <div class="col-md-2">
                 <div class="panel panel-default panel-primary">
                     <div class="panel-heading">
                         <h3 class="panel-title">
                            EBank Application
                         </h3>
                     </div>                       
                     <ul class="nav list-group">
                         <li>@Html.ActionLink(“<application name 1>”, "Index", “<Data Source Name 1>”)</li>
                         <li>@Html.ActionLink(“<application name 2>”, "Index", “<Data Source Name 2>”)</li>
                     </ul>                        
                 </div>
             </div>              
             <div class="col-md-10">
                 @RenderBody()                   
             </div>
         </div>        
        </div>
  5. Build the solution.

    A single sign on page is created for accessing two host applications based on different data source projects.