创建自定义安全令牌提供程序
<|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)
最新文章
- 汽车安全气囊系统保护乘客安全
- 汽车湿度传感器:守护车内舒适环境的关键小部件
- 涡轮增压技术解析:原理、性能对比与未来趋势
- 电动汽车电池寿命与续航:关键影响因素与保养指南
- 汽车制动系统安全性能检测方法
- 汽车蓄电池极板硫化:原因、危害及预防处理全解析
- 真皮座椅保养全攻略:清洁+去渍+季节养护技巧
- 灯光信号险解析:车损险与第三者责任险如何搭配更划算
- 电动汽车三大核心技术突破:电池、充电与电控系统革新
- 硅基负极:突破动力电池续航瓶颈的关键技术
- 智能液晶屏革新车险服务:车损险显示技术解析与未来展望
- 汽车四轮定位全解析:前束角与外倾角调整指南
- 电动化与智能化双轮驱动:电池技术、激光雷达与车联网重塑未来出行
- Axios拦截器原理:链式调用与请求响应流程解析
- 汽车与未来出行的革新之路
- 激光雷达与毫米波雷达驱动自动驾驶技术革新与商业化落地
- 车险理赔全攻略:从报案到维修的20个关键细节
- 涡轮增压+缸内直喷+可变气门:三大核心技术提升发动机动力
- 车险理赔全流程指南:从定损到维修的完整解析
- 热传导影响汽车发动机散热效率
