Opc.Ua.Server.SessionManager.ActivateSession C# (CSharp) Method

ActivateSession() public method

Activates an existing session
public ActivateSession ( OperationContext context, NodeId authenticationToken, SignatureData clientSignature, List clientSoftwareCertificates, ExtensionObject userIdentityToken, SignatureData userTokenSignature, StringCollection localeIds, byte &serverNonce ) : bool
context OperationContext
authenticationToken NodeId
clientSignature SignatureData
clientSoftwareCertificates List
userIdentityToken ExtensionObject
userTokenSignature SignatureData
localeIds StringCollection
serverNonce byte
return bool
        public virtual bool ActivateSession(
            OperationContext                context,
            NodeId                          authenticationToken,
            SignatureData                   clientSignature,
            List<SoftwareCertificate>       clientSoftwareCertificates,
            ExtensionObject                 userIdentityToken,
            SignatureData                   userTokenSignature,
            StringCollection                localeIds,
            out byte[]                      serverNonce)
        {
            serverNonce = null;
            
            Session session = null;
            UserIdentityToken newIdentity = null;
            UserTokenPolicy userTokenPolicy = null;

            lock (m_lock)
            {
                // find session.
                if (!m_sessions.TryGetValue(authenticationToken, out session))
                {
                    throw new ServiceResultException(StatusCodes.BadSessionClosed);
                }
                
                // create new server nonce.
                serverNonce = new byte[m_minNonceLength];
                m_nonceGenerator.GetBytes(serverNonce);

                // validate before activation.
                session.ValidateBeforeActivate(
                    context,
                    clientSignature,
                    clientSoftwareCertificates,
                    userIdentityToken,
                    userTokenSignature,
                    localeIds,
                    serverNonce,
                    out newIdentity,
                    out userTokenPolicy);
            }
            
            IUserIdentity identity = null;
            IUserIdentity effectiveIdentity = null;
            ServiceResult error = null;

            try
            {
                // check if the application has a callback which validates the identity tokens.
                lock (m_eventLock)
                {
                    if (m_ImpersonateUser != null)
                    {
                        ImpersonateEventArgs args = new ImpersonateEventArgs(newIdentity, userTokenPolicy);
                        m_ImpersonateUser(session, args);
                        
                        if (ServiceResult.IsBad(args.IdentityValidationError))
                        {
                            error = args.IdentityValidationError;
                        }
                        else
                        {
                            identity = args.Identity;
                            effectiveIdentity = args.EffectiveIdentity;
                        }
                    }
                }

                // parse the token manually if the identity is not provided.
                if (identity == null)
                {
                    identity = new UserIdentity(newIdentity);
                }

                // use the identity as the effectiveIdentity if not provided.
                if (effectiveIdentity == null)
                {
                    effectiveIdentity = identity;
                }
            }
            catch (Exception e)
            {
                if (e is ServiceResultException)
                {
                    throw;
                }

                throw ServiceResultException.Create(
                    StatusCodes.BadIdentityTokenInvalid, 
                    e, 
                    "Could not validate user identity token: {0}", 
                    newIdentity);
            }

            // check for validation error.
            if (ServiceResult.IsBad(error))
            {
                throw new ServiceResultException(error);
            }

            // activate session.
            bool contextChanged = session.Activate(
                context,
                clientSoftwareCertificates,
                newIdentity,
                identity,
                effectiveIdentity,
                localeIds,
                serverNonce);

            // raise session related event.
            if (contextChanged)
            {
                RaiseSessionEvent(session, SessionEventReason.Activated); 
            }

            // indicates that the identity context for the session has changed.
            return contextChanged;       
        }