UserManager.GetCurrentPrincipal() Method
Gets the current user’s ClaimsPrincipal
object.
Namespace: DevExpress.ExpressApp.Security
Assembly: DevExpress.ExpressApp.Security.v24.1.dll
NuGet Package: DevExpress.ExpressApp.Security
Declaration
Returns
Type | Description |
---|---|
IPrincipal | An object that implements the |
Remarks
The following code snippet demonstrates a custom authentication provider implementation that uses the GetCurrentPrincipal
method to access the ClaimsPrincipal
object returned by a third-party authentication service (XAF Solution Wizard generates equivalent code for applications configured to use OAuth2).
public class CustomAuthenticationProvider : IAuthenticationProviderV2 {
private readonly UserManager userManager;
public CustomAuthenticationProvider(UserManager userManager) {
this.userManager = userManager;
}
public object Authenticate(IObjectSpace objectSpace) {
var currentPrincipal = userManager.GetCurrentPrincipal();
if(currentPrincipal?.Identity?.IsAuthenticated ?? false) {
var user = userManager.FindUserByPrincipal<ApplicationUser>(objectSpace, currentPrincipal);
if(user != null) {
return new UserResult<ApplicationUser>(user);
}
// The code below creates users for testing purposes only.
#if !RELEASE
bool autoCreateUser = true;
if(autoCreateUser) {
var userResult = userManager.CreateUser<ApplicationUser>(objectSpace, currentPrincipal, user => {
user.Roles.Add(objectSpace.FirstOrDefault<PermissionPolicyRole>(role => role.Name == "Default"));
});
if(!userResult.Succeeded) {
//throw userResult.Error;
}
return userResult;
}
#endif
}
return null;
}
}
See Also