Example #1
0
        /// <summary>
        /// Returns user id of the user represented by 'logOnName'.
        /// </summary>
        /// <param name="token">AuthenticatedToken of the admin, in case id of any user is to be retrieved. </param>
        /// <param name="logOnName">LogOnName of the user whose id is to be returned.</param>
        /// <returns>Id of the user represented by the token.</returns>
        /// <remarks>Call this method when an admin wants to retrieve id of any user. For a non-admin user who wants to
        /// retrieve his id, call other overload with just token parameter.</remarks>
        /// <example>
        /// <code>
        /// //Get authentication provider from factory
        /// IAuthenticationProvider provider = AuthenticationProviderFactory.CreateAuthenticationProvider(&quot;ZentityAuthenticationProvider&quot;);
        /// //Login as admin
        /// AuthenticatedToken adminToken = provider.Authenticate(new UserNameSecurityToken(&quot;Administrator&quot;, &quot;XXXX&quot;));//Supply correct password
        /// //Admin retrieves Jimmy's id
        /// Guid id = ZentityUserManager.GetUserId(adminToken, &quot;Jimmy&quot;);
        /// Console.WriteLine(&quot;id = {0}&quot;, id);
        /// </code>
        /// </example>
        /// <seealso cref="Zentity.Security.AuthenticationProvider.ZentityAuthenticationProvider"/>
        public static Guid GetUserId(AuthenticatedToken token, string logOnName)
        {
            //// Input Validation
            ValidateToken(token);
            ValidateParameters("logOnName", logOnName);

            try
            {
                //// The token must be of the user who is updating the logon name or it must be an admin's token.
                if (!string.Equals(token.IdentityName, logOnName, StringComparison.OrdinalIgnoreCase) &&
                    !DataAccessLayer.IsAdmin(token.IdentityName))
                {
                    return(Guid.Empty);
                }

                ZentityUser user = new ZentityUser(logOnName, token);
                Guid        id   = user.GetUserId();
                return(id);
            }
            catch (TypeInitializationException ex)
            {
                //// thrown in case of incorrect application configuration
                throw new AuthenticationException(ConstantStrings.TypeInitializationExceptionMessage, ex);
            }
        }
Example #2
0
        /// <summary>
        ///  Returns user id
        /// </summary>
        /// <param name="logOnName">Log on name of the user</param>
        /// <param name="password">Password of the user</param>
        /// <returns>user id</returns>
        /// <example>
        /// Pre-requisites for running this code sample
        /// <list type="bullet">
        /// <item>Refer to the sample application configuration file given in help, and create a similar one for your application.</item>
        /// <item>Add reference to Zentity.Security.Authentication.dll and Zentity.Security.AuthenticationProvider.dll </item>
        /// <item>Run the sample for registering new users to create the user accounts in the authentication database.</item>
        /// <item>Then run this sample, replacing inputs with valid values</item>
        /// </list>
        /// <code>
        /// try
        ///    {
        ///        Guid userId = ZentityUserManager.GetUserId(&quot;JohnDE&quot;, &quot;john@123&quot;);
        ///        if (userId != Guid.Empty)
        ///        {
        ///            Console.WriteLine(&quot;Guid is {0}&quot;, userId);
        ///        }
        ///        else
        ///        {
        ///            Console.WriteLine(&quot;Guid could not be retrieved. Please check the user credentials&quot;);
        ///        }
        ///    }
        ///    catch (AuthenticationException ex)
        ///    {
        ///        Console.WriteLine(ex.Message);
        ///        //In case of database errors the AuthenticationException object will wrap the sql exception.
        ///        if (ex.InnerException != null)
        ///        {
        ///            Console.WriteLine(ex.InnerException.Message);
        ///        }
        ///    }
        ///
        /// </code>
        /// </example>
        public static Guid GetUserId(string logOnName, string password)
        {
            #region Input Validation
            ValidateParameters("logOnName", logOnName, "password", password);
            #endregion

            try
            {
                ZentityUser user = new ZentityUser(logOnName, password);
                Guid        id   = user.GetUserId();
                return(id);
            }
            catch (TypeInitializationException ex)
            {
                //// thrown in case of incorrect application configuration
                throw new AuthenticationException(ConstantStrings.TypeInitializationExceptionMessage, ex);
            }
        }