When I attempt to update the Description of a user on the Domain Controller, I get the error UnauthorizedAccessException. I have validated the username and password I am using is in fact able to update the user. I logged in as the services username and password and manually changing the data. Here is my code.
private PrincipalContext pc = new PrincipalContext(ContextType.Domain, Common.DCData.serverName, null, ContextOptions.Negotiate, Common.DCData.userName, Common.DCData.password );
public bool Save(UserData data)
{
try
{
UserPrincipal up = UserPrincipal.FindByIdentity(pc, data.userName);
if ((data.DisplayName != null) && (data.DisplayName != "") && (data.DisplayName != up.DisplayName))
up.DisplayName = data.DisplayName;
if ((data.givenName != null) && (data.givenName != "") && (data.givenName != up.GivenName))
up.GivenName = data.givenName;
if ((data.middleName != null) && (data.middleName != "") && (data.middleName != up.MiddleName))
up.MiddleName = data.middleName;
if ((data.surname != null) && (data.surname != "") && (data.surname != up.Surname))
up.Surname = data.surname;
if ((data.emailAddress != null) && (data.emailAddress != "") && (data.emailAddress != up.EmailAddress))
up.EmailAddress = data.emailAddress;
if ((data.voiceTelephoneNumber != null) && (data.voiceTelephoneNumber != "") && (data.voiceTelephoneNumber != up.VoiceTelephoneNumber))
up.VoiceTelephoneNumber = data.voiceTelephoneNumber;
if ((data.description != null) && (data.description != "") && (data.description != up.Description))
up.Description = data.description;
up.Save(pc);
return true;
}
catch(Exception ex)
{
// Some logging goes here
}
}
The Common.DCData is a simple class that gets data from a database. I have double checked the username and password in the database
The static class I created to pull the data from SQL was not instantiated automatically (as I thought it would be) when referencing the static member. I ended up making the class a non-static class, and instantiating the class manually before using it.
Related
if (user.IsBot == false && (user.Nickname.ToString().Equals(buffer) || user.Username.ToString().Equals(buffer)))
{
await ReplyAsync(Context.User.Mention + message[new Random().Next(5)] + user.Mention);
}
If user doesn't have a nickname it says Object reference not set to an instance of an object.
You should check if Nickname is null first. In this case I would propose something like:
if (user.Nickname != null && user.IsBot == false)
{
if (user.Nickname.ToString().Equals(buffer) || user.Username.ToString().Equals(buffer)))
{
await ReplyAsync(Context.User.Mention + message[new Random().Next(5)] + user.Mention);
}
}
Or even:
if (user != null)
{
if (user.Nickname != null && user.IsBot == false)
{
if (user.Nickname.ToString().Equals(buffer) || user.Username.ToString().Equals(buffer)))
{
await ReplyAsync(Context.User.Mention + message[new Random().Next(5)] + user.Mention);
}
}
}
if user can be null too.
I am trying to fetch all account entity fields from an organization.(https://democrm365.crm4.dynamics.com).
Also, I created some custom fields in account and added into form. After that when I run the following code I am getting all fields related to account.
IOrganizationService service = (IOrganizationService)serviceProxy;
RetrieveEntityRequest request = new RetrieveEntityRequest()
{
EntityFilters = EntityFilters.Attributes,
RetrieveAsIfPublished = false,
LogicalName = "account"
};
RetrieveEntityResponse res = (RetrieveEntityResponse)service.Execute(request);
EntityMetadata currentEntity = res.EntityMetadata;
foreach (AttributeMetadata attribute in currentEntity.Attributes)
{
if (attribute.DisplayName.UserLocalizedLabel != null && attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
{
if (_allowedFieldTypes.Contains(attribute.AttributeType.ToString().ToLower()))
{
EntityField ef = new EntityField();
ef.AttributeType = attribute.AttributeType.ToString() ?? "";
ef.DisplayName = attribute.DisplayName.UserLocalizedLabel.Label;
ef.IsCustomField = attribute.IsCustomAttribute ?? false;
ef.IsAllowUpdate = attribute.IsValidForUpdate ?? false;
ef.LogicalName = attribute.LogicalName;
if (attribute.AttributeType.ToString().ToLower() == "picklist")
{
PicklistAttributeMetadata pm = (PicklistAttributeMetadata)attribute;
foreach (OptionMetadata x in pm.OptionSet.Options)
{
ef.Items.Add(x.Label.UserLocalizedLabel.Label);
}
}
else if (attribute.AttributeType.ToString().ToLower() == "virtual")
{
if (attribute.AttributeTypeName.Value == "MultiSelectPicklistType")
{
MultiSelectPicklistAttributeMetadata pm = (MultiSelectPicklistAttributeMetadata)attribute;
foreach (OptionMetadata x in pm.OptionSet.Options)
{
ef.Items.Add(x.Label.UserLocalizedLabel.Label);
}
}
}
if (Add)
{
fieldLst.Add(ef);
}
}
}
}
Again, I tested the same code on different organization (https://zoho5.crm.dynamics.com) with all above mentioned steps, then
below code is not working.
if (attribute.DisplayName.UserLocalizedLabel != null && attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
attribute.DisplayName.UserLocalizedLabel is null for all fields like (Account Name, Account No. Etc)
After some test runs, I removed the custom fields from account form and publish form. Then the above code is working fine.
Try this. You may have to get the label from LocalizedLabels when UserLocalizedLabel is null.
foreach (AttributeMetadata attribute in currentEntity.Attributes)
{
if (attribute.AttributeType != null && attribute.LogicalName != "" && attribute.AttributeType != null && attribute.IsValidForRead.Value == true)
{
string attributeName = attribute.LogicalName;
if (attribute.DisplayName.UserLocalizedLabel != null)
{
attributeName = attribute.DisplayName.UserLocalizedLabel.Label;
}
if (attributeName == attribute.LogicalName && attribute.DisplayName.LocalizedLabels.Count > 0)
{
attributeName = attribute.DisplayName.LocalizedLabels[0].Label;
}
}
}
What is the Query results window's global service (interface)? Code below:
var dteService = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
if (dteService == null)
{
Debug.WriteLine("");
return;
}
var something=Package.GetGlobalService(typeof(???)) as ???;
EDIT: The goal is, when I press the context menu button, I want the function callback to be able to access the service where the work item is selected (or the results list
Please check this case in MSDN forum for the details how to get it work: https://social.msdn.microsoft.com/Forums/vstudio/en-US/2d158b9c-dec1-4c59-82aa-f1f2312d770b/sdk-packageget-selected-item-from-query-results-list
The following code is quoted from above link for your quick reference:
Document activeDocument = _applicationObject.ActiveDocument;
if (activeDocument != null)
{
DocumentService globalService = (DocumentService)Package.GetGlobalService(typeof(DocumentService));
if (globalService != null)
{
string fullName = activeDocument.FullName;
IWorkItemTrackingDocument document2 = globalService.FindDocument(fullName, null);
if ((document2 != null) && (document2 is IResultsDocument))
{
int[] selectedItemIds = ((IResultsDocument)document2).SelectedItemIds;
}
}
}
var dteService = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
if (dteService == null)
{
Debug.WriteLine("");
return;
}
DocumentService documentService = Package.GetGlobalService(typeof(DocumentService)) as DocumentService;
if (documentService == null)
return;
string fullName = dteService.ActiveDocument.FullName;
IWorkItemTrackingDocument activeDocument = documentService.FindDocument(fullName, null);
if (activeDocument == null || !(activeDocument is IResultsDocument))
return;
I have a signup page that have many filed. Many of them should filled by user.
I use RequiredFieldValidator and RegularExpressionValidator for validate in client side.
Should I validate them in server side? How?
I wrote this code. I use many if and else if. Is this correct?
CaptchaControl1.ValidateCaptcha(txtSecureImg.Text);
if (CaptchaControl1.UserValidated)
{
if (txtFName.Text != string.Empty && txtLName.Text != string.Empty && txtUserName.Text != string.Empty && txtEmail.Text != string.Empty && txtPass.Text != string.Empty && txtCPass.Text != string.Empty && txtSecureImg.Text != string.Empty)
{
if (RegEx.EmailValidate(txtEmail.Text) == 1 && RegEx.PasswordValidate(txtPass.Text) == 1 && RegEx.UserName(txtUserName.Text) == 1)
{
try
{
// insert in database
}
catch (Exception)
{
lblMsg.Text = "Error";
}
}
else if(RegEx.EmailValidate(txtEmail.Text) == 0)
{
EmailRegularExpression.Visible = true;
}
else if(RegEx.PasswordValidate(txtPass.Text) == 0)
{
passRegularExpression.Visible = true;
}
else if(RegEx.UserName(txtUserName.Text) == 0)
{
UnameRegularExpression.Visible = true;
}
}
else if(txtFName.Text == string.Empty)
{
RequiredFieldValidator1.Visible = true;
}
// continue like above for another filed
}
else
{
lblMsg.Text = "Please insert Secure Image";
}
And :
public static int EmailValidate(string Mail)
{
int i = 0;
Regex regExEmail = new Regex(#"\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (regExEmail.IsMatch(Mail))
i = 1;
return i;
}
I'm assuming this is Web Forms...
The validation is run automatically. You can just use the Page.IsValid property: msdn
You'll still need to manually check the captcha field though.
CaptchaControl1.ValidateCaptcha(txtSecureImg.Text);
if (CaptchaControl1.UserValidated && Page.IsValid)
{
// Insert in db.
}
Since you use those validators, then you don't need to validate form in server side again like your codes.
But you should call Page.Validate()and then check page with Page.IsValid method.
from here
example
private PMS_USERS currUser;
private bool validateUserName()
{
dbContext = new PmsEntities();
var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF).Where(p=> p.USERNAME == currUser.USERNAME);
return !validateUser.Any();
}
Hello,
I got an error while validating on my new user register form.
My PMS_USERS table has no record(null). I also tried checking for null control(s) for currUser.
What am I missing?
Error is :
Non static method requires a target
You should first test if currUser is null or not and your dbContext too.
if (currUser == null) return false;
if (dbContext == null) throw new Exception ("The dbContext has not been set");
Secondly, you can simplify your query like yhat :
var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF && p.USERNAME == currUser.USERNAME);
And then change the return statement to :
return (validateUser.FirstOrDefault() != null);
You can alternativelly use SingleOrDefault statement insead of FirstOrDefault, if you want to be sure there is only one user corresponding to your criteria.
"Non static method requires a target" means that some object inside the scope is null.
Try checking the context and the var result values:
dbContext = new PmsEntities();
if (dbContext != null && currUser != null)
{
var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF && p.USERNAME == currUser.USERNAME);
if (validateUser !=null)
{
return !validateUser.Any();
}
else
return null;
}
Check it and tell us if you have the same exception.
Use
private PMS_USERS currUser;
private bool validateUserName()
{
dbContext = new PmsEntities();
return PMS_USERS != null
? var validateUser = dbContext.PMS_USERS.Where(p=> p.LOGICALREF != currUser.LOGICALREF).Where(p=> p.USERNAME == currUser.USERNAME).Any()
: false;
}