文章 单点登录插件

单点登录(SSO)插件允许您替换 Limyee 电商平台的身份验证过程  如果您有一个自己自定义身份验证过程的外部应用程序,则可以使用单点登录插件来强制 Limyee 电商平台使用您的自定义身份验证流程。

何时应创建自定义 SSO 插件?

仅当以下扩展不能满足应用程序需求时,才需要创建自定义 SSO 扩展。 但是,在大多数情况下,应用程序(尤其是 Web 应用程序)可以通过更改其身份验证代码来编写所需的 Cookie,从而使用 Cookie 身份验证 SSO 扩展。

以下扩展已作为 Limyee 电商平台核心产品的一部分,并且可以在管理 ->身份验证区域中配置

Cookies 认证

这允许用户通过第三方 cookie 登录。 外部应用程序将创建 cookie,Limyee 电商平台将识别它并用于对用户进行身份验证。 此模块适用于大多数基于Web的应用程序,该应用程序应能够编写和加密 cookie,并且与您的 Limyee电商平台存在于同一父域中。

Forms 身份验证

这特定于 ASP.NET 站点。 默认情况下,Limyee 电商平台使用 Forms 身份验证,因此,它能够读取该身份验证 Cookie 或与使用 Forms 身份验证的其他 ASP.net 站点共享该身份验证 Cookie,前提是每个站点都配置了相同的身份验证。

创建 SSO 扩展

IAuthenticationPlugin 接口是由这一种方法组成:

public int? GetAuthenticatedUserId(AuthenticationOptions options)
{

AuthenticationOptions 对象包装了应用程序请求关联的 HttpContext。我们可以提取它并在继续之前进行健全性检查。

    HttpContextBase context = options.HttpContext;
    if (context == null)
        return null;

HttpContext 应该包含您需要知道的所有内容,以提取用户请求的身份。下面的实现代码是基于您的系统及其存储标识的方式。

    // For instance, a certain cookie...
    HttpCookie cookie = context.Request.Cookies["CookieName"];
    if (cookie == null)
        return null;

    // or the context ASP.net user.
    if (context.User == null || context.User.Identity == null || string.IsNullOrEmpty(context.User.Identity.Name))
        return null;

一旦你使用 Limyee 电商平台 API 查找到用户所需的信息,你就继续执行代码了。如果未找到该用户,则可以在此处使用 API,在 Limyee 电商平台中创建新的用户帐户。作为使 Limyee 电商平台帐户与外部系统保持同步的方法,这可能是有用的。否则,您只需抛出一个错误,指示登录尝试失败。

    var userApi = Apis.Get<IUsers>();

    User foundUser = userApi.Get(new UsersGetOptions { Username = "username" });

    if (foundUser == null)
    {
        // If the user doesn't exist, you may want to create an account
        // if you are trying to stay synced with another system.

        // If not, you can throw an exception here about the login failing.
        throw new Exception("Login Failed");
    }

一旦用户被检索,该方法要求您返回用户的ID,以供 Limyee 电商平台进一步使用并完成请求。

    // Assuming a user was found, return the id for Community to use.
    return foundUser.Id;
}

下面是完整的示例实现:

using System;
using System.Web;
using Limyee.Extensibility;
using Limyee.Extensibility.Api.Entities.Version1;
using Limyee.Extensibility.Api.Version1;
using Limyee.Extensibility.Security.Version2;

namespace Sample
{
    class SampleAuthenticationPlugin : IAuthenticationPlugin
    {
        #region IPlugin Implementation

        public string Name
        {
            get { return "Sample Authentication Single-Sign-On Client"; }
        }

        public string Description
        {
            get { return "Sample IAuthentication plugin implementation."; }
        }

        public void Initialize()
        {
            // Leave blank if not needed.
        }

        #endregion

        public int? GetAuthenticatedUserId(AuthenticationOptions options)
        {
            // Check the HttpContext from the given options
            HttpContextBase context = options.HttpContext;
            if (context == null)
                return null;

            // Use the context to extract the needed information about the accessing user.
            // For instance, from a certain cookie...
            HttpCookie cookie = context.Request.Cookies["CookieName"];
            if (cookie == null)
                return null;

            // or from the context ASP.net user.
            if (context.User == null || context.User.Identity == null || string.IsNullOrEmpty(context.User.Identity.Name))
                return null;

            // Extract information to identify the user.

            // Access the User API and find the user based on the information retrieved above.
            var userApi = Apis.Get<IUsers>();

            User foundUser = userApi.Get(new UsersGetOptions { Username = "username" });

            if (foundUser == null)
            {
                // If the user doesn't exist, you may want to create an account
                // if you are trying to stay synced with another system.

                // If not, you can throw an exception here about the login failing.
                throw new Exception("Login Failed");
            }

            // Assuming a user was found, return the id for Community to use.
            return foundUser.Id;
        }
    }
}

使用外部登录过程

IExternalAuthenticationPlugin 接口添加了一些额外的 url 属性,允许使用自定义/外部页面进行登录控制。当您希望帐户管理过程的导航由第三方系统管理时,请使用此类型。包含了 ReturnUrl 回调,因此您可以在登录后将用户导航回 Limyee 电商平台请注意,使用 IConfigurablePlugin 允许在不重新启动站点的情况下,管理和编辑这些属性是非常有效的。

此界面上的大多数属性都指定 Limyee 电商平台应将用户发送到哪些 URL 以完成各种操作:登录、注销、重置密码、创建帐户等。

public string ChangePasswordUrl
{
    get { return ""; }
}

public string CreateUserUrl
{
    get { return ""; }
}

public string ForgottenPasswordUrl
{
    get { return ""; }
}

public string LoginUrl
{
    get { return ""; }
}

public string LogoutUrl
{
    get { return ""; }
}

最后一个属性指定 Limyee 电商平台将使用查询字符串键添加到返回 URL,然后调用您的应用程序。在登录(或其他)过程完成后,将用户返回到此 URL。

public string ReturnUrlParameter
{
    get { return ""; }
}