WSE 3.0 and Custom Token Manager

Hello,

We have made an application in Visual Studio 2003 with WSE 2.0. Now we have try to convert it to Visual Studio 2005 with WSE 3.0.

When we are running the application we get the following error:

WSE032: There was an error loading the microsoft.web.services3 configuration section. > System.Configuration.ConfigurationErrorsException: WSE040: Type ZKHNet.Model.Services.CustomUsernameTokenManager, WebServices could not be loaded. Please check the configuration file. (C:\\ZKHNet\\WebServices\\web.config line 49)

Our web.config file has the following section:

<microsoft.web.services3>
<
security>
<
securityTokenManager>
<
addlocalname="UsernameToken"type="ZKHNet.Model.Services.CustomUsernameTokenManager, WebServices"namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"/>
</
securityTokenManager>
</
security>
</
microsoft.web.services3>

Our CustomUserNameTokenmanger looks as follows:

using System;
using Microsoft.Web.Services3.Security.Tokens;
using System.Security.Permissions;
namespace ZKHNet.Model.Services
{
///<summary>
///
Summary description for CustomUsernameTokenManager.
///</summary>

[SecurityPermission(SecurityAction.Demand, Flags =SecurityPermissionFlag.UnmanagedCode)]

public classCustomUsernameTokenManager :UsernameTokenManager
{
public CustomUsernameTokenManager()
{
//
// TODO: Add constructor logic here
//
}

protected override string AuthenticateToken(UsernameToken token)
{
string strWebServiceKey = System.Configuration.
ConfigurationSettings.AppSettings["Security.WebServiceKey"];
if (token.Password == strWebServiceKey)
{
return token.Password;
}
else
{
return token.Password +
" invalid";
}
}
}
}

The class CustomUserNameTokenmanger is in the same project as the web.config file.

Does anybody know what we are doing wrong because this was working in VS2003.

Thanks for your time and answer

Devriendt Nico

[9835 byte] By [NicoDevriendt] at [2007-12-17]
# 1
You should have a web.config file that looks like this

<security>
<securityTokenManager>
<!-- You can optionally choose to supply the name of the dll (WebServices) in which to find the CustomUsernameTokenManager type -->
<!-- If no dll name is supplied the CustomUsernameTokenManager is assumed to be in the the \bin directory -->
<add type="ZKHNet.Model.Services.CustomUsernameTokenManager, WebServices" namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" localName="UsernameToken" xmlns=""/>
</securityTokenManager>
</security>

Did you load the WSE 2.0 project via VS2005 as it should ensure that your config file is correct?

Thanks, Mark Fussell

MarkFussell at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 2
Ensure that the WebServices dll still exists.

We ran into the same error messages, because our custom UsernameTokenManager class was included in a code behind file. Asp.net 1.1 compiles all code behind files into a single dll that is placed in the Bin directory. ASP.Net 2.0 uses multiple dlls and doesn't place them in the Bin directory.

We solved the problem by moving the custom UsernameTokenManager to its own Class Library project and then referencing that project from the webservice project. That way the custom UsernameTokenManager sits in a dll in the Bin directory.

HTH

MarkArnott at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 3

I have exactly the same problem.

Creating a seperate project and placing the CustomUsernameTokenManager in there and changing the web.config of the web service made no difference.

Same error!!!!

Does anyone have the answer to this bug?

Ultrasoft at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 4
Hi!

I had to burn one of my MSDN support incidents, but it was really worth it (they helped me even if WSE2.0 is not officially supported in VS2005).

I was informed that I should replace "COMPANY.PROGRAM.Services" with

"App_Code".

<securityTokenManager type="COMPANY.PROGRAM.Services.MyUsernameTokenManager,

App_Code"

xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" qname="wsse:UsernameToken" />

I hope you have use of this information when trying to reference WSE2.0 from

a VS2005 project.

Best regards

Benjamin
Sweden

BenjaminTengelin at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 5

Here's one of the solution:

You must put the CustomTokenManager's dll file into
GAC (Global Assembly Cache) which located into "C:\WINDOWS\assembly"

--Example--

Step 1: Create a project named "CustomClass" using VS. studio 2005
Step 2: Create the CustomToken manager code as below:

Imports System
Imports System.Xml
Imports System.Security.Permissions
Imports System.Web.Security
Imports System.Security.Principal

Imports Microsoft.Web.Services3.Security
Imports Microsoft.Web.Services3.Security.Tokens

Namespace Service

<SecurityPermissionAttribute(SecurityAction.Demand, Flags:=SecurityPermissionFlag.UnmanagedCode)> _
Public Class CustomUsernameTokenManager
Inherits UsernameTokenManager

Public Sub New()
End Sub

Public Sub New(ByRef nodes As XmlNodeList)
MyBase.New(nodes)
End Sub

Protected Overrides Function AuthenticateToken(ByVal token As UsernameToken) As String
Dim blnValidCredential As Boolean = False
Dim objIdentity As GenericIdentity
Dim objPrincipal As GenericPrincipal
Dim strRole As String()

If token.Username = "JohnDoe" And token.Password = "johnd123" Then
Else
Throw New ApplicationException("Can not authenticate this user")
End If

strRole = New String() {"GuestUser"}
objIdentity = New GenericIdentity(token.Username)
objPrincipal = New GenericPrincipal(objIdentity, strRole)
token.Principal = objPrincipal

Return token.Password

End Function

End Class

End Namespace

Step 3: Before compile the project to produce the "CustomClass.dll",
you must follow the instruction from this link
(How to install an assembly in the Global Assembly Cache):

http://support.microsoft.com/default.aspx?scid=kb;en-us;315682

Step 4: Record the "AssemblyName", "Version", "Culture", "Public Key Token"
in the "C:\WINDOWS\assembly"

Step 5: Go to "Web.config" in the Server Side Web Services project
and add what you recorded in the ""C:\WINDOWS\assembly" like
the following sample:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
......

<system.web>

<compilation debug="true" strict="false" explicit="true">
<assemblies>
<add assembly="CustomClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=10697CC0ABF25E2F"/>
</assemblies>
</compilation>

</system.web>
<microsoft.web.services3>
<policy fileName="wse3policyCache.config"/>
<security>
<securityTokenManager>
<add localName="UsernameToken" type="CustomClass.Service.CustomUsernameTokenManager, CustomClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=10697CC0ABF25E2F" namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"/>
</securityTokenManager>
</security>
</microsoft.web.services3>
</configuration>


That's all folk.

To.Ph.

dafan at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 6

Sorry, but that is not the solution. I made the steps, but the custom usernametokenmanager isn`t loaded...

Are you sure, that the name of the VS-Project (here:CustomClass) is important for the securityTokenManager-path in web.config ?

My projectname: CustomClass

My namespace in the CustomUsernameTokenManager.cs class: CustomClass

So i need this path: <add type="CustomClass.CustomClass.CustomUsernameTokenManager, CustomClass...

Regards

M.

KennyKls at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 7

Kenny - did you ever figure out usernameTokenManager?

I get ConfigurationErrorsException - could not load type (my custom implementation of usernameTokenManager).

I can send you my sample if you need to see it.

RAchmann at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...
# 8

Mark - or anyone.

Did anyone get this custom usernameTokenManager stuff to work?

My custom type can't load wihen I instatiate the ws proxy in my client app.

RAchmann at 2007-9-8 > top of Msdn Tech,.NET Development,ASMX Web Services and XML Serialization...

.NET Development

Site Classified