Skip to main content
All docs
V25.1
  • .NET 8.0+
    • The page you are viewing does not exist in the .NET Framework 4.6.2+ platform documentation. This link will take you to the parent topic of the current section.

    SignInManager.AuthenticateByLogonParameters(Object) Method

    Authenticates a user based on the specified logon parameters.

    Namespace: DevExpress.ExpressApp.Security

    Assembly: DevExpress.ExpressApp.Security.v25.1.dll

    NuGet Package: DevExpress.ExpressApp.Security

    Declaration

    public AuthenticationResult AuthenticateByLogonParameters(
        object logonParameters
    )

    Parameters

    Name Type Description
    logonParameters Object

    An object that contains a user’s logon parameters.

    Returns

    Type Description
    DevExpress.ExpressApp.Security.AuthenticationResult

    An object of the AuthenticationResult type that contains the result of an authentication attempt.

    Remarks

    Use this method to programmatically authenticate a user in an XAF application. This method takes an object that contains a user’s logon parameters and returns an AuthenticationResult object.

    The returned object exposes the following properties.

    Succeeded
    A boolean property that indicates whether or not the authentication attempt was successful.
    Principal
    If the authentication succeeds, this property contains the ClaimsPrincipal (a collection of statements about the authenticated user) returned by the Security System.
    Error
    If the authentication fails, this property contains the resulting Exception.

    Example

    The code sample below demonstrates how you can use the AuthenticateByLogonParameters method to implement a Backend Web API Service controller for JSON Web Token (JWT)-based authentication. The Template Kit generates equivalent code for Blazor projects with integrated Web API.

    File: MySolution.Blazor\API\Security\AuthenticationController.cs

    using DevExpress.ExpressApp.Security;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.IdentityModel.Tokens;
    using Swashbuckle.AspNetCore.Annotations;
    using System.IdentityModel.Tokens.Jwt;
    using System.Text;
    
    namespace MySolution.WebApi.Jwt;
    
    [ApiController]
    [Route("api/[controller]")]
    public class AuthenticationController : ControllerBase {
        readonly SignInManager signInManager;
        readonly IConfiguration configuration;
    
        public AuthenticationController(SignInManager signInManager, IConfiguration configuration) {
            this.signInManager = signInManager;
            this.configuration = configuration;
        }
    
        [HttpPost("Authenticate")]
        public IActionResult Authenticate(
            [FromBody]
                [SwaggerRequestBody(@"For example: <br /> { ""userName"": ""Sam"", ""password"": """" }")]
                AuthenticationStandardLogonParameters logonParameters
        ) {
            var authenticationResult = signInManager.AuthenticateByLogonParameters(logonParameters);
    
            if(authenticationResult.Succeeded) {
                var issuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Authentication:Jwt:IssuerSigningKey"]!));
                var token = new JwtSecurityToken(
                    issuer: configuration["Authentication:Jwt:ValidIssuer"],
                    audience: configuration["Authentication:Jwt:ValidAudience"],
                    claims: authenticationResult.Principal.Claims,
                    expires: DateTime.Now.AddHours(2),
                    signingCredentials: new SigningCredentials(issuerSigningKey, SecurityAlgorithms.HmacSha256)
                    );
                return Ok(new JwtSecurityTokenHandler().WriteToken(token));
            }
            return Unauthorized("User name or password is incorrect.");
        }
    }
    

    Usage Considerations

    • The AuthenticateByLogonParameters method differs from the SignInByLogonParameters method in the following ways:
      • The AuthenticateByLogonParameters method only uses the passed logon parameters to find a user. This method does not affect the currently logged in user.
      • The SignInByLogonParameters method implicitly calls the AuthenticateByLogonParameters method to find a user. If the user is found, SignInByLogonParameters signs in this user.
    See Also