public static ClientContext GetContext(string webUrl, string username) { string userName = username; string password = ""; UserPass userinfo = Helpers.CredentialHelper.GetCredentials("zalodevcreds"); if (userinfo == null) { Console.WriteLine("User: "******"\r\n"); password = GetPassword(); Helpers.CredentialHelper.SetCredentials(new UserPass() { UserName = username, Password = password }, "zalodevcreds"); } else { password = userinfo.Password; username = userinfo.UserName; } var secure = new SecureString(); foreach (char c in password) { secure.AppendChar(c); } ClientContext ctx = new ClientContext(webUrl); ctx.Credentials = ctx.Credentials = new SharePointOnlineCredentials(userName, secure); return(ctx); }
public static bool SetCredentials(UserPass up, string CredentialName) { var cm = new Credential { Target = CredentialName, PersistanceType = PersistanceType.Enterprise, Username = up.UserName, Password = up.Password }; return(cm.Save()); }
public static UserPass GetCredentials(string CredentialName) { var cm = new Credential { Target = CredentialName }; if (!cm.Exists()) { return(null); } cm.Load(); var up = new UserPass(cm); return(up); }