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
您需要将返回对象强制转换为变量的对象类型:
DNNServiceSecurityToken token = (DNNServiceSecurityToken) DataCache.GetCache("DNNServiceSecurityToken_" + TokenId.ToString());
根据错误提示尝试投射它:
DNNServiceSecurityToken token = DataCache.GetCache("DNNServiceSecurityToken_" + TokenId.ToString()) as DNNServiceSecurityToken;
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.