2014-09-02 129 views
0

首先,我知道这属于“不推荐实践”类别,但我有要求通过ASP.Net网站将Word文档转换为PDF,并且我一直在使用Word Interop,因为它是免费的,易于实现,而且Word已经安装在服务器上。使用Interop将Word转换为PDF需要管理员权限

当我一直在测试,它是为我工作,但发布后用户报告“未经授权的访问”的错误。我有服务器的管理权限,我发现在服务器上添加用户作为管理员可以工作,但我不想授予每个用户的管理员权限。

因此,有很多事情需要发生,有没有其他替代方案,免费,用于将Word文档转换为PDF的库,我可以使用?是否有一种简单的方式让我的用户访问Interop库而不授予管理员权限?有没有一种方法可以模拟Web应用程序这部分的管理员用户,因为应用程序需要Windows身份验证?

还有什么我没有想过的东西可以让我受益吗?

+0

免费被高估了。如果您购买价值1000美元的解决方案,但可以节省数天的研究,编码,调试和支持工作,这非常值得花费。 – 2014-09-02 14:13:17

+0

不幸的是我不在财务决策部门,所以自由是唯一不幸的方法。 – anothershrubery 2014-09-02 14:32:38

+0

或者你可以向决策者解释这一点。 – 2014-09-02 15:01:02

回答

2

您可以使用模拟来以不同的用户身份运行代码。在CodeProject上有一个很好的模拟类,http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User

只需在机器上创建一个新的管理员帐户,即可进行自己想要的操作,将凭证存储在应用程序设置的web.config中(如果您想要使用rijandael并使用代码进行解密,请将其加密)

但从长期储存总之,你使用它像这样,

using (new Impersonator("myUsername", "myDomainname", "myPassword")) 
{ 
    //This code is running elevated as the specified user 
} 
//This code is reverted back to the previous user. 

其实,我写我自己的web.config的ConfigurationElement存储凭据。它看起来像这样,

public class CredentialConfigurationElement : ConfigurationElement 
{ 
    #region Properties 
    [ConfigurationProperty("userName", DefaultValue="", IsRequired=true)] 
    public string UserName 
    { 
     get { return (string)this["userName"];} 
     set { this["userName"] = value; } 
    } 

    [ConfigurationProperty("password", DefaultValue = "", IsRequired = true)] 
    public string Password 
    { 
     get { return (string)this["password"]; } 
     set { this["password"] = value; } 
    } 
    #endregion 

    #region Explicit Operators 
    public static implicit operator NetworkCredential(CredentialConfigurationElement value) 
    { 
     return new NetworkCredential(value.UserName, value.Password); 
    } 
    public static implicit operator CredentialConfigurationElement(NetworkCredential value) 
    { 
     return new CredentialConfigurationElement() { UserName = value.UserName, Password = value.Password }; 
    } 
    #endregion 
} 

但是,要使用它,您需要创建一个Custom ConfigurationSection,这是一个从ConfigurationSection继承的类,并将CredentialConfigurationElement作为属性公开。

E.g.您可以创建一个名为CodeImpersonatorSection的新节:ConfigurationSection

并在此处将CredentialConfigurationElement公开为名为ImpersonationCredentials的属性。

然后使用(CodeImpersonatorSection)WebConfigurationManager.GetSection(“/ yoursectionnamehere”);获取配置实例。

有选择地修改冒领类自动做到这一点,并改变它有一个静态方法类似

Impersonator.RunUnderImpersonation(p => { 
    //This code is impersonating the configured user. 
}); 
+0

我用这个相同的代码来模拟一个没有**拥有管理权限的用户,所以我可以尝试单步执行代码,但是它仍然工作,所以我认为'Impersonator'代码没有工作。使用它来模仿管理员似乎按预期工作,这很好。干杯。 – anothershrubery 2014-09-02 14:51:02

+0

您是如何调试代码,在IIS中连接到w3wp.exe,还是使用f5和IIS Express? – 2014-09-02 14:53:07

+0

IIS Express,没有尝试附加到发布的代码。但它现在正在工作,所以很好。 – anothershrubery 2014-09-02 15:40:28