Reputation: 840
I'm trying to convert some VB.net code I found online to some C# code but I'm getting the following error around this line of code in my C# code:
DNNServiceSecurityToken token = DataCache.GetCache("DNNServiceSecurityToken_" + TokenId.ToString());
The specific error code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Dispatcher;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;
using MyTypes;
public class SecurityTokenInspector : IParameterInspector
{
public string Roles;
public SecurityTokenInspector(string value)
{
Roles = value;
}
public object BeforeCall(string operationName, object[] inputs)
{
// token will always be the last parameter
int index = inputs.Length - 1;
string TokenId = inputs[index].ToString();
// ********** ERROR LINE BELOW HERE *********** //
// THE ERROR IS HAPPENING WITH THIS LINE: //
// ****** first make sure token exists ****** //
DNNServiceSecurityToken token = DataCache.GetCache("DNNServiceSecurityToken_" + TokenId.ToString());
if (token == null)
{
throw new Exception("Security Token Expired. Please request a new Token");
}
// if token exists, check user roles
UserInfo user = UserController.GetUserById(0, token.UserID);
if (!(user.IsInRole(Roles)))
{
throw new Exception("Access Denied. Role Membership Requirements not met");
return null;
}
return null;
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
return;
}
}
Imports System
Imports System.ServiceModel.Dispatcher
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Users
Public Class SecurityTokenInspector
Implements IParameterInspector
Public Roles As String
Public Sub New(ByVal value As String)
Roles = value
End Sub
Public Function BeforeCall(ByVal operationName As String, _
ByVal inputs() As Object) As Object _
Implements IParameterInspector.BeforeCall
' token will always be the last parameter
Dim index As Integer = inputs.Length - 1
Dim TokenId As String = inputs(index).ToString()
' first make sure token exists
Dim token As DNNServiceSecurityToken =
_DataCache.GetCache("DNNSecurityToken_" & TokenId)
If token Is Nothing Then
Throw New Exception( _
"Security Token Expired. Please request a new Token")
End If
' if token exists, check user roles
Dim user As UserInfo = UserController.GetUserById(0, token.UserID)
If Not user.IsInRole(Roles) Then
Throw New Exception( _
"Access Denied. Role Membership Requirements not met")
Return Nothing
End If
Return Nothing
End Function
Public Sub AfterCall(ByVal operationName As String, _
ByVal outputs() As Object, ByVal returnValue As Object, _
ByVal correlationState As Object) _
Implements IParameterInspector.AfterCall
Return
End Sub
End Class
Upvotes: 1
Views: 1757
Reputation: 2185
Do you really need the token to be a DNNServiceSecurityToken? Or will the type returned by GetCache work just as well?
Try this:
var token = DataCache.GetCache("DNNServiceSecurityToken_" + TokenId.ToString());
Upvotes: 1
Reputation: 5095
You need to cast the return object to the object type of your variable:
DNNServiceSecurityToken token = (DNNServiceSecurityToken) DataCache.GetCache("DNNServiceSecurityToken_" + TokenId.ToString());
Upvotes: 4
Reputation: 3645
Try casting it as the error suggests:
DNNServiceSecurityToken token = DataCache.GetCache("DNNServiceSecurityToken_" + TokenId.ToString()) as DNNServiceSecurityToken;
Upvotes: 2