我创建了一个控制台应用程序,用于在visual studio中获取SharePoint网站标题。 SharePoint站点的URL在代码中传递,但密码需要在运行时的控制台窗口中输入。
然后我定义了构建定义,并且能够构建解决方案并将文件复制到工件中。
我配置了发布定义,我正在尝试运行build-ed .exe文件,但是收到了以下错误:
Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file.
PS:我已经定义了一个变量 $ pass ,然后将其添加到参数部分以获取SharePoint网站的密码。
控制台应用的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace VSTS_Demo_App
{
class Program
{
static void Main(string[] args)
{
string userName = "$$$@$$$.onmicrosoft.com";
Console.WriteLine("Enter your password.");
SecureString password = GetPassword();
// ClienContext - Get the context for the SharePoint Online Site
// SharePoint site URL - https://c986.sharepoint.com
using (var clientContext = new ClientContext("https://$$$$$$$$$$$$$$$$$$$$$$$$$$"))
{
// SharePoint Online Credentials
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
// Get the SharePoint web
Web web = clientContext.Web;
// Load the Web properties
clientContext.Load(web);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Web properties - Display the Title and URL for the web
Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);
Console.ReadLine();
}
}
private static SecureString GetPassword()
{
ConsoleKeyInfo info;
Console.WriteLine("get passwod -called");
//Get the user's password as a SecureString
SecureString securePassword = new SecureString();
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter)
{
securePassword.AppendChar(info.KeyChar);
}
}
while (info.Key != ConsoleKey.Enter);
return securePassword;
}
}
}
答案 0 :(得分:0)
问题在于Console.ReadLine()方法。 运行构建时,vsts会重定向控制台输入和输出。
根据您提供的堆栈跟踪,您的代码在info = Console.ReadKey(true);
打破了。
您通过args传递它,但您没有使用它。 将您的代码更改为:
SecureString password;
if (args.length > 0)
{
password = args;
}
else
{
password = GetPassword();
}
我没有测试password = args是否可以直接工作,或者你是否需要追加字符。我将留给你。
答案 1 :(得分:0)
像这样更新你的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace VSTS_Demo_App
{
class Program
{
static void Main(string[] args)
{
string userName = "$$$@$$$.onmicrosoft.com";
Console.WriteLine("Enter your password.");
SecureString password = GetPassword(args);
// ClienContext - Get the context for the SharePoint Online Site
// SharePoint site URL - https://c986.sharepoint.com
using (var clientContext = new ClientContext("https://$$$$$$$$$$$$$$$$$$$$$$$$$$"))
{
// SharePoint Online Credentials
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
// Get the SharePoint web
Web web = clientContext.Web;
// Load the Web properties
clientContext.Load(web);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Web properties - Display the Title and URL for the web
Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);
//Console.ReadLine();
}
}
private static SecureString GetPassword(string[] args)
{
ConsoleKeyInfo info;
Console.WriteLine("get passwod -called");
//Get the user's password as a SecureString
SecureString securePassword = new SecureString();
if (args.length > 0)
{
char[] cs= args[0].ToCharArray();
foreach(char c in cs)
{
securePassword.AppendChar(c);
}
}
else
{
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter)
{
securePassword.AppendChar(info.KeyChar);
}
}
while (info.Key != ConsoleKey.Enter);
}
return securePassword;
}
}
}