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.

    UserManager.GetAuthenticationToken<TUser>(TUser, Int32, IEnumerable<Claim>) Method

    Gets an authentication token for the specified user.

    Namespace: DevExpress.ExpressApp.Security

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

    NuGet Package: DevExpress.ExpressApp.Security

    Declaration

    public string GetAuthenticationToken<TUser>(
        TUser user,
        int expirationSeconds,
        IEnumerable<Claim> additionalClaims = null
    )
        where TUser : class, ISecurityUserWithLoginInfo

    Parameters

    Name Type Description
    user TUser

    An application user object.

    expirationSeconds Int32

    The number of seconds until the authentication token expires.

    Optional Parameters

    Name Type Default Description
    additionalClaims IEnumerable<Claim> null

    A collection of additional claims to add to the resulting token.

    Type Parameters

    Name Description
    TUser

    The user object type.

    Returns

    Type Description
    String

    A System.Stringthat contains the resulting authentication topic.

    Remarks

    Important

    This method is not supported on the WinForms platform. If called from a WinForms application, it throws the PlatformNotSupportedException.

    The following code snippet demonstrates a custom implementation of an IAuthenticationTokenProviderthat uses the GetAuthenticationToken to generate an authentication token for a successfully authenticated user:

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Core;
    using DevExpress.ExpressApp.Security;
    using DevExpress.ExpressApp.Security.Authentication.ClientServer;
    using System.Runtime.ExceptionServices;
    // ...
    public class JwtTokenProviderService : IAuthenticationTokenProvider {
        IServiceProvider serviceProvider;
    
        public JwtTokenProviderService(IServiceProvider serviceProvider) {
            this.serviceProvider = serviceProvider;
        }
        public string Authenticate(object logonParameters) {
            var signInManager = serviceProvider.GetRequiredService<SignInManager>();
            var userManager = serviceProvider.GetRequiredService<UserManager>();
    
            var result = signInManager.AuthenticateByLogonParameters(logonParameters);
            if(result.Succeeded) {
                using IObjectSpace nonSecuredObjectSpace = serviceProvider
                    .GetRequiredService<INonSecuredObjectSpaceFactory>().CreateNonSecuredObjectSpace<ApplicationUser>();
                var user = userManager.FindUserByPrincipal<ApplicationUser>(nonSecuredObjectSpace, result.Principal);
                var token = userManager.GetAuthenticationToken(user, 6000);
                return token;
            }
            if(result.Error is IUserFriendlyException) {
                ExceptionDispatchInfo.Throw(result.Error);
            }
            throw new AuthenticationException("Internal server error");
        }
    }
    
    See Also