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
Gets an authentication token for the specified user.
Namespace : DevExpress.ExpressApp.Security
Assembly : DevExpress.ExpressApp.Security.v25.2.dll
NuGet Package : DevExpress.ExpressApp.Security
public string GetAuthenticationToken<TUser>(
TUser user,
int expirationSeconds,
IEnumerable<Claim> additionalClaims = null
)
where TUser : class, ISecurityUserWithLoginInfo
Public Function GetAuthenticationToken(Of TUser As {Class, ISecurityUserWithLoginInfo})(
user As TUser,
expirationSeconds As Integer,
additionalClaims As IEnumerable(Of Claim) = Nothing
) As String
| Name | Type | Description |
|---|---|---|
| user | TUser |
An application user object.
| | expirationSeconds | Int32 |
The number of seconds until the authentication token expires.
|
| Name | Type | Default | Description |
|---|---|---|---|
| additionalClaims | IEnumerable<Claim> | null |
A collection of additional claims to add to the resulting token.
|
| Name | Description |
|---|---|
| TUser |
The user object type.
|
| Type | Description |
|---|---|
| String |
A System.Stringthat contains the resulting authentication topic.
|
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