Agregue o quite licencias para que el usuario habilite o deshabilite el uso de ofertas en la nube de Microsoft a las que la empresa tiene licencias. Por ejemplo, una organización puede tener una suscripción Microsoft 365 Enterprise E3 con 100 licencias y esta solicitud asigna una de esas licencias a un usuario específico. También puede habilitar y deshabilitar planes específicos asociados a una suscripción. El método de licencias de usuario directo es una alternativa a las licencias basadas en grupos.
Elija el permiso o los permisos marcados como con privilegios mínimos para esta API. Use un permiso o permisos con privilegios superiores solo si la aplicación lo requiere. Para obtener más información sobre los permisos delegados y de aplicación, consulte Tipos de permisos. Para obtener más información sobre estos permisos, consulte la referencia de permisos.
Tipo de permiso
Permisos con privilegios mínimos
Permisos con privilegios más altos
Delegado (cuenta profesional o educativa)
LicenseAssignment.ReadWrite.All
Directory.ReadWrite.All, User.ReadWrite.All
Delegado (cuenta personal de Microsoft)
No admitida.
No admitida.
Aplicación
LicenseAssignment.ReadWrite.All
Directory.ReadWrite.All, User.ReadWrite.All
Importante
En escenarios delegados con cuentas profesionales o educativas, al usuario que ha iniciado sesión se le debe asignar un rol de Microsoft Entra compatible o un rol personalizado con el permiso de microsoft.directory/users/assignLicense rol. Se admiten los siguientes roles con privilegios mínimos para esta operación:
Escritores de directorios
Administrador de licencias
Administrador de usuarios
Solicitud HTTP
POST /users/{id | userPrincipalName}/assignLicense
Colección de objetos assignedLicense que especifican las licencias que se van a agregar. Puede deshabilitar servicePlans asociado a una licencia estableciendo la propiedad disabledPlans en un objeto assignedLicense .
removeLicenses
Colección Guid
Colección de skuIds que identifican las licencias que se van a quitar. Obligatorio. Puede ser una colección vacía.
Respuesta
Si se ejecuta correctamente, este método devuelve un código de respuesta 200 OK y el objeto user en el cuerpo de la respuesta.
Ejemplos
Ejemplo 1: Asignación de licencias al usuario que ha iniciado sesión
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Me.AssignLicense;
using Microsoft.Graph.Models;
var requestBody = new AssignLicensePostRequestBody
{
AddLicenses = new List<AssignedLicense>
{
new AssignedLicense
{
DisabledPlans = new List<Guid?>
{
Guid.Parse("8a256a2b-b617-496d-b51b-e76466e88db0"),
},
SkuId = Guid.Parse("84a661c4-e949-4bd2-a560-ed7766fcaf2b"),
},
new AssignedLicense
{
DisabledPlans = new List<string>
{
},
SkuId = Guid.Parse("f30db892-07e9-47e9-837c-80727f46fd3d"),
},
},
RemoveLicenses = new List<string>
{
},
};
// To initialize your graphClient, see https://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.AssignLicense.PostAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.users.item.assignlicense.AssignLicensePostRequestBody assignLicensePostRequestBody = new com.microsoft.graph.users.item.assignlicense.AssignLicensePostRequestBody();
LinkedList<AssignedLicense> addLicenses = new LinkedList<AssignedLicense>();
AssignedLicense assignedLicense = new AssignedLicense();
LinkedList<UUID> disabledPlans = new LinkedList<UUID>();
disabledPlans.add(UUID.fromString("8a256a2b-b617-496d-b51b-e76466e88db0"));
assignedLicense.setDisabledPlans(disabledPlans);
assignedLicense.setSkuId(UUID.fromString("84a661c4-e949-4bd2-a560-ed7766fcaf2b"));
addLicenses.add(assignedLicense);
AssignedLicense assignedLicense1 = new AssignedLicense();
LinkedList<String> disabledPlans1 = new LinkedList<String>();
assignedLicense1.setDisabledPlans(disabledPlans1);
assignedLicense1.setSkuId(UUID.fromString("f30db892-07e9-47e9-837c-80727f46fd3d"));
addLicenses.add(assignedLicense1);
assignLicensePostRequestBody.setAddLicenses(addLicenses);
LinkedList<String> removeLicenses = new LinkedList<String>();
assignLicensePostRequestBody.setRemoveLicenses(removeLicenses);
var result = graphClient.me().assignLicense().post(assignLicensePostRequestBody);
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.users.item.assign_license.assign_license_post_request_body import AssignLicensePostRequestBody
from msgraph.generated.models.assigned_license import AssignedLicense
# To initialize your graph_client, see https://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = AssignLicensePostRequestBody(
add_licenses = [
AssignedLicense(
disabled_plans = [
UUID("8a256a2b-b617-496d-b51b-e76466e88db0"),
],
sku_id = UUID("84a661c4-e949-4bd2-a560-ed7766fcaf2b"),
),
AssignedLicense(
disabled_plans = [
],
sku_id = UUID("f30db892-07e9-47e9-837c-80727f46fd3d"),
),
],
remove_licenses = [
],
)
result = await graph_client.me.assign_license.post(request_body)
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Me.AssignLicense;
using Microsoft.Graph.Models;
var requestBody = new AssignLicensePostRequestBody
{
AddLicenses = new List<AssignedLicense>
{
},
RemoveLicenses = new List<Guid?>
{
Guid.Parse("f30db892-07e9-47e9-837c-80727f46fd3d"),
Guid.Parse("84a661c4-e949-4bd2-a560-ed7766fcaf2b"),
},
};
// To initialize your graphClient, see https://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.AssignLicense.PostAsync(requestBody);
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
requestBody := graphusers.NewItemAssignLicensePostRequestBody()
addLicenses := []graphmodels.AssignedLicenseable {
}
requestBody.SetAddLicenses(addLicenses)
removeLicenses := []uuid.UUID {
uuid.MustParse("f30db892-07e9-47e9-837c-80727f46fd3d"),
uuid.MustParse("84a661c4-e949-4bd2-a560-ed7766fcaf2b"),
}
requestBody.SetRemoveLicenses(removeLicenses)
// To initialize your graphClient, see https://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
assignLicense, err := graphClient.Me().AssignLicense().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.users.item.assignlicense.AssignLicensePostRequestBody assignLicensePostRequestBody = new com.microsoft.graph.users.item.assignlicense.AssignLicensePostRequestBody();
LinkedList<AssignedLicense> addLicenses = new LinkedList<AssignedLicense>();
assignLicensePostRequestBody.setAddLicenses(addLicenses);
LinkedList<UUID> removeLicenses = new LinkedList<UUID>();
removeLicenses.add(UUID.fromString("f30db892-07e9-47e9-837c-80727f46fd3d"));
removeLicenses.add(UUID.fromString("84a661c4-e949-4bd2-a560-ed7766fcaf2b"));
assignLicensePostRequestBody.setRemoveLicenses(removeLicenses);
var result = graphClient.me().assignLicense().post(assignLicensePostRequestBody);
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\AssignLicense\AssignLicensePostRequestBody;
use Microsoft\Graph\Generated\Models\AssignedLicense;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new AssignLicensePostRequestBody();
$requestBody->setAddLicenses([ ]);
$requestBody->setRemoveLicenses(['f30db892-07e9-47e9-837c-80727f46fd3d', '84a661c4-e949-4bd2-a560-ed7766fcaf2b', ]);
$result = $graphServiceClient->me()->assignLicense()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Users.Actions
$params = @{
addLicenses = @(
)
removeLicenses = @(
"f30db892-07e9-47e9-837c-80727f46fd3d"
"84a661c4-e949-4bd2-a560-ed7766fcaf2b"
)
}
# A UPN can also be used as -UserId.
Set-MgUserLicense -UserId $userId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.users.item.assign_license.assign_license_post_request_body import AssignLicensePostRequestBody
from msgraph.generated.models.assigned_license import AssignedLicense
# To initialize your graph_client, see https://video2.skills-academy.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = AssignLicensePostRequestBody(
add_licenses = [
],
remove_licenses = [
UUID("f30db892-07e9-47e9-837c-80727f46fd3d"),
UUID("84a661c4-e949-4bd2-a560-ed7766fcaf2b"),
],
)
result = await graph_client.me.assign_license.post(request_body)