// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Identity
{
///
/// Provides methods to create a claims principal for a given user.
///
/// The type used to represent a user.
/// The type used to represent a role.
public class UserClaimsPrincipalFactory : IUserClaimsPrincipalFactory
where TUser : class
where TRole : class
{
///
/// Initializes a new instance of the class.
///
/// The to retrieve user information from.
/// The to retrieve a user's roles from.
/// The configured .
public UserClaimsPrincipalFactory(
UserManager userManager,
RoleManager roleManager,
IOptions optionsAccessor)
{
if (userManager == null)
{
throw new ArgumentNullException(nameof(userManager));
}
if (roleManager == null)
{
throw new ArgumentNullException(nameof(roleManager));
}
if (optionsAccessor == null || optionsAccessor.Options == null)
{
throw new ArgumentNullException(nameof(optionsAccessor));
}
UserManager = userManager;
RoleManager = roleManager;
Options = optionsAccessor.Options;
}
///
/// Gets the for this factory.
///
///
/// The current for this factory instance.
///
public UserManager UserManager { get; private set; }
///
/// Gets the for this factory.
///
///
/// The current for this factory instance.
///
public RoleManager RoleManager { get; private set; }
///
/// Gets the for this factory.
///
///
/// The current for this factory instance.
///
public IdentityOptions Options { get; private set; }
///
/// Creates a populated for the specified .
///
/// The user instance to create claims on.
/// A that represents the started task.
public virtual async Task CreateAsync(TUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
var userId = await UserManager.GetUserIdAsync(user);
var userName = await UserManager.GetUserNameAsync(user);
var id = new ClaimsIdentity(IdentityOptions.ApplicationCookieAuthenticationType,
Options.ClaimsIdentity.UserNameClaimType,
Options.ClaimsIdentity.RoleClaimType);
id.AddClaim(new Claim(Options.ClaimsIdentity.UserIdClaimType, userId));
id.AddClaim(new Claim(Options.ClaimsIdentity.UserNameClaimType, userName));
if (UserManager.SupportsUserSecurityStamp)
{
id.AddClaim(new Claim(Options.ClaimsIdentity.SecurityStampClaimType,
await UserManager.GetSecurityStampAsync(user)));
}
if (UserManager.SupportsUserRole)
{
var roles = await UserManager.GetRolesAsync(user);
foreach (var roleName in roles)
{
id.AddClaim(new Claim(Options.ClaimsIdentity.RoleClaimType, roleName));
if (RoleManager.SupportsRoleClaims)
{
var role = await RoleManager.FindByNameAsync(roleName);
if (role != null)
{
id.AddClaims(await RoleManager.GetClaimsAsync(role));
}
}
}
}
if (UserManager.SupportsUserClaim)
{
id.AddClaims(await UserManager.GetClaimsAsync(user));
}
return new ClaimsPrincipal(id);
}
}
}