创建自定义安全令牌提供程序
<|begin▁of▁sentence|>---
title: "How to: Create a Custom Security Token Provider"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "security [WCF], providing credentials"
ms.assetid: db8cb478-aa43-478b-bf97-c6489ad7c7fd
---
# How to: Create a Custom Security Token Provider
This topic describes how to create a new security token provider types and how to integrate the provider with a custom security token manager.
> [!NOTE]
> Create a custom token provider if the tokens provided by the system in the namespace do not meet your requirements.
The security token provider creates a security token representation based on information in the client or service credentials. To use the custom security token provider in Windows Communication Foundation (WCF) security, you must create custom credentials and security token manager implementations.
For more information about custom credentials and security token manager, see [Walkthrough: Creating Custom Client and Service Credentials](../../../../docs/framework/wcf/extending/walkthrough-creating-custom-client-and-service-credentials.md).
For more information about credentials, security token manager, and provider and authenticator classes, see [Security Architecture](https://msdn.microsoft.com/library/16593476-d36a-408d-808c-ae6fd483e28f).
### To create a custom security token provider
1. Define a new class derived from the class.
2. Implement the method. The method is responsible for creating and returning an instance of the security token. The following example creates a class named `MySecurityTokenProvider`, and overrides the method to return an instance of the class. The class constructor requires an instance of the class.
[!code-csharp[c_CustomTokenProvider#1](../../../../samples/snippets/csharp/VS_Snippets_CFX/c_customtokenprovider/cs/source.cs#1)]
[!code-vb[c_CustomTokenProvider#1](../../../../samples/snippets/visualbasic/VS_Snippets_CFX/c_customtokenprovider/vb/source.vb#1)]
### To integrate a custom security token provider with a custom security token manager
1. Define a new class derived from the class. (The following example derives from the class, which derives from the class.)
2. Override the method if it is not already overridden.
The method is responsible for returning an instance of the class appropriate for the parameter passed to the method by the WCF security framework. Modify the method to return the custom security token provider implementation (created in the previous procedure) when the method is called with an appropriate token parameter. For more information about the security token manager, see [Walkthrough: Creating Custom Client and Service Credentials](../../../../docs/framework/wcf/extending/walkthrough-creating-custom-client-and-service-credentials.md).
3. Add custom logic to the method to enable it to return the custom security token provider based on the parameter. The following example returns the custom security token provider if the token requirements are satisfied. The requirements include an X.509 security token and message direction (using the token for message output). In all other cases, the code calls the base class to maintain the system-provided behavior.
[!code-csharp[c_CustomTokenProvider#2](../../../../samples/snippets/csharp/VS_Snippets_CFX/c_customtokenprovider/cs/source.cs#2)]
[!code-vb[c_CustomTokenProvider#2](../../../../samples/snippets/visualbasic/VS_Snippets_CFX/c_customtokenprovider/vb/source.vb#2)]
## Example
The following shows a complete implementation along with the corresponding implementation.
[!code-csharp[c_CustomTokenProvider#0](../../../../samples/snippets/csharp/VS_Snippets_CFX/c_customtokenprovider/cs/source.cs#0)]
[!code-vb[c_CustomTokenProvider#0](../../../../samples/snippets/visualbasic/VS_Snippets_CFX/c_customtokenprovider/vb/source.vb#0)]
## See Also
[Walkthrough: Creating Custom Client and Service Credentials](../../../../docs/framework/wcf/extending/walkthrough-creating-custom-client-and-service-credentials.md)
[How to: Create a Custom Security Token Authenticator](../../../../docs/framework/wcf/extending/how-to-create-a-custom-security-token-authenticator.md)
[Security Architecture](https://msdn.microsoft.com/library/16593476-d36a-408d-808c-ae6fd483e28f)
最新文章
- 汽车维修厂横梁加固方案:钢板与碳纤维如何选择?
- 发动机过热冷却系统故障需检查冷却液和风扇
- 宝马驾驶体验卓越操控精准
- 未来汽车三大科技革命:自动驾驶、固态电池与智能座舱
- 电动化浪潮席卷全球,智能化与轻量化引领汽车产业新变革
- 安全车距保障行车平稳
- 北汽新能源引领绿色出行
- 涡轮增压技术解析:原理、性能对比与未来趋势
- 2023汽车科技四大突破:智能驾驶与电动化引领行业变革
- 车损险全面解析:碰撞事故、自然灾害与盗抢险保障
- 智能座舱革新+车路协同突破:2023汽车科技三大趋势解析
- 智能驾驶与新能源革命:未来汽车技术发展全景
- 动力电池与激光雷达引领新能源汽车智能化革命
- 高压平台与自动驾驶:技术解析与安全冗余设计
- 固态电池突破:高能量密度与10分钟快充技术解析
- "揭秘:为何普通跑车永远开不出法拉利的激情?"
- 高精地图赋能智能汽车:激光雷达与线控系统的未来驾驶革命
- 固态电池突破:能量密度提升50%,充电速度翻3倍
- 智能网联汽车:自动驾驶与车路协同技术引领未来出行变革
- 3D点云技术重塑汽车数字建模新纪元
