Back to Devexpress

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

expressappframework-devexpress-dot-expressapp-dot-security-dot-usermanager-dot-getauthenticationtoken-1-x28-0-system-dot-int32-system-dot-collections-dot-generic-dot-ienumerable-system-dot-security-dot-claims-dot-claim-x29.md

latest4.3 KB
Original Source

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.2.dll

NuGet Package : DevExpress.ExpressApp.Security

Declaration

csharp
public string GetAuthenticationToken<TUser>(
    TUser user,
    int expirationSeconds,
    IEnumerable<Claim> additionalClaims = null
)
    where TUser : class, ISecurityUserWithLoginInfo
vb
Public Function GetAuthenticationToken(Of TUser As {Class, ISecurityUserWithLoginInfo})(
    user As TUser,
    expirationSeconds As Integer,
    additionalClaims As IEnumerable(Of Claim) = Nothing
) As String

Parameters

NameTypeDescription
userTUser

An application user object.

| | expirationSeconds | Int32 |

The number of seconds until the authentication token expires.

|

Optional Parameters

NameTypeDefaultDescription
additionalClaimsIEnumerable<Claim>null

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

|

Type Parameters

NameDescription
TUser

The user object type.

|

Returns

TypeDescription
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:

csharp
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

UserManager Class

UserManager Members

DevExpress.ExpressApp.Security Namespace