Azure QnaMaker chat logs are not generated - qnamaker

The GenerateAnswer API describes userId if sent in a request will be recorded in chat logs which as far as I could see do not seem to get generated.
Do chat logs get generated by default? or am I required to provision resources or make configuration changes for logging to take effect?
And please can you point me to Azure documentation if any that describes the logging aspects of QnA Maker in detail?
Thanks

QnA Maker does not generate chatlogs by default. They currently do not offer any APIs to store chat logs.
Assuming you are using it in a form of a chatbot where question and answers are displayed concurrently, you could create a database instance that is stored locally such as SQL Server 2016.
Then by generating a unique GUID based on time of user visit store each chatlog in a table with the Primary key as 'GUID' followed by 'Question' and 'Answer' and 'Time'.
Example of potential answer model
namespace Frontend.Data {
public class Message {
[Key]
public string MessageId { get; set; }
public string ConversationId { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
public DateTime MessageReceivedAtDateTime { get; set; }
public Message(string messageId, string conversationId, string question, string answer, string datestring) {
MessageId = messageId;
ConversationId = conversationId;
Answer = answer;
Question = question;
MessageReceivedAtDateTime = DateTimeOffset.Parse(datestring).UtcDateTime;
}
public Message() {}
}
However, you may have to tinker around with saving the question and answers as the user exits the program.
Depending on the language you are implementing QnA Maker, you may need to tweak some of these changes
https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb?view=sql-server-2017
https://learn.microsoft.com/en-us/dotnet/api/system.guid.newguid?view=netframework-4.7.2

Related

ASP.NET Core 3.1 Web API: how to protect sensitive data from return with model?

I have a Posts model class that has a relation with Users model.
When I call API to get Posts with the owner of it (user), it returns with all user info including password.
How to prevent model from returning sensitive information like passwords?
You should create new classes that you return from your actions. Only include the fields/information you want to return.
These classes are also known as Data Transfer Objects (DTO).
You can use [JsonIgnore] to avoid serializing the property value:
public class Users
{
public int Id { get; set; }
[System.Text.Json.Serialization.JsonIgnore]
public string Password{ get; set; }
//...
}

API testing using post man

I am new to api testing from postman.I went through several blogs and articles on
api testing.But I don't know how deeper we can done testing.
Also how can we write test for post request.I have the below department mode class
from my web api.
[Key]
public int DepartmentId { get; set; }
[Required]
[Display(Name ="Department Name")]
public string DepartmentName { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
The sample get request i used is given below
var body = JSON.parse(responseBody);
if(!body.DepartmentId)
{
tests["department id must exists in response"]=true;
}
else
{
tests["department id exists in response"]=true;
}
if(typeof body.DepartmentName !='string')
{
tests["department name must be type string"]=true;
}
if(responseCode.name.has("OK"))
{
tests["Status code name has string OK"] = true;
}
Whether the above testing procedure is correct.?
What all things to be tested while invoking a post request and get request against department controller for the above model.
You dont have to write 2 test cases to test for +ve and -ve scenario. All you need to do is write one test case and it will either pass or fail based on the scenario. For example lets consider your DepartmentId
The test case should be
tests["department id must exists in response"]= body.DepartmentId? true : false;
Based on whether you department id exists or not either your test case will pass (marked green) or fail (marked red).
Alternatively if you are dealing with not only testing but also designing your APIs and documenting them then have a look at http://myapic.com. Its a great tool for end to end API design documenting and testing.

Gathering Active Directory Data for MVC intranet application

I need assistance with gathering Active Directory data based on a table in my DB. I have an entity class that holds user requests. Each request has the user's windows name from System.Web.HttpContext.Current.User.Identity.Name. My problem is I cannot figure out how to setup a linq query to associate the AD username to the rest of the AD so I can display their full names instead of their username in my table. Here is what I have so far, any help will be appreciated.
public partial class RequestInfo
{
public int RequestInfoId { get; set; }
public string RequestByUserADId { get; set; }
public System.DateTime RequestDateTime { get; set; }
public string Explanation { get; set; }
public virtual UserInfo UserInfo { get; set; } // where I define my custom roles
}
I can query AD by using the code below. I have tried Get Active Directory User Information With Windows Authentication in MVC 4, but it did not help.
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
using (UserPrincipal user = UserPrincipal.FindByIdentity(context, requestByAdId))
{
return user.DisplayName
}
I may be off here because I am not sure if you are able to successful establish a user principal or not but if you have the user principal you can get property information like the following:
user.GetProperty("propertyName")
Here is a static method that should get you the department for a user, for example.
public static String GetDepartment(UserPrincipal principal)
{
return principal.GetProperty("department");
}
Let me know where this gets you and I can elaborate further if this isn't working.
Edit
It appears you need to go one level deeper to get the fields that aren't by default a part of the user principal. For this you will need to get the directory entry from the user principal first:
DirectoryEntry directoryEntry = (userPrincipal.GetUnderlyingObject() as DirectoryEntry);
Then you need to check if the attribute you are looking for exists, and if it does, get the value. A great way to do this is to create a helper method that you pass your directory entry to along with the string value for the property name that you want to get.
public string GetProperty(DirectoryEntry directoryEntry, string propertyName)
{
if (directoryEntry.Properties.Contains(propertyName))
{
return directoryEntry.Properties[propertyName][0].ToString();
}
else
{
return string.Empty;
}
}
Please note that going to the underlying object is expensive. I believe this operation, by default, is cached for you so subsequent use of this information can be retrieved from cache. Playing around with
directoryEntry.RefreshCache
will get you started with that.
Let me know if this does the trick for you!

loosing dataAnottation when upload model from database

I have a big database existing database to comunicate with, and I'm using EF 5.0 database first, the problem I'm having is that if I create any data decoration like [stringlength(50)] on the class and then the databases is uploaded, when I "upload from database" all data annotations are gone. How can I do to keep them?
It's very simple: You Can't! Because those codes are auto-generated and will be over written on each model update or change.
However you can achieve what you need through extending models. Suppose that EF generated the following entity class for you:
namespace YourSolution
{
using System;
using System.Collections.Generic;
public partial class News
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int UserID { get; set; }
public virtual UserProfile User{ get; set; }
}
}
and you want do some work arounds to preserve your you data annotations and attributes. So, follow these steps:
First, add two classes some where (wherever you want, but it's better to be in Models) like the following:
namespace YourSolution
{
[MetadataType(typeof(NewsAttribs))]
public partial class News
{
// leave it empty.
}
public class NewsAttribs
{
// Your attribs will come here.
}
}
then add what properties and attributes you want to the second class - NewsAttribs here. :
public class NewsAttrib
{
[Display(Name = "News title")]
[Required(ErrorMessage = "Please enter the news title.")]
public string Title { get; set; }
// and other properties you want...
}
Notes:
1) The namespace of the generated entity class and your classes must be the same - here YourSolution.
2) your first class must be partial and its name must be the same as EF generated class.
Go through this and your attribs never been lost again ...
The accepted answer may work for standard data operations, but I am trying to validate the model prior to the call to DbSet.Add using TryValidateObject. With the accepted answer, it is still not picking up on the data annotations.
What did work for me I found in a .NET Runtime GitHub thread, as proposed by what I'm inferring is one of the .NET developers.
Basically, this is a bug, and you have to force the model to recognize the metadata decorations using TypeDescriptor.AddProviderTransparent . . .
TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(News), typeof(NewsAttrib)), typeof(News));
Once I make this call, TryValidateObject recognizes the data annotations and returns false when any of the constraints are not met.
Here's the link. I little more than half-way down, there's a working code sample in a .zip file.
https://github.com/dotnet/runtime/issues/46678

MVC4 WebApi OData Delta<T> is not populating GUIDs or Int fields

I'm looking to do partial updates on a web api controller action by using the Delta wrapper.
I have a model like such:
public class Person
{
public Guid PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsActive { get; set; }
public int NumVacationDays { get; set; }
public double Salary { get; set; }
}
I have the api controller like such:
public void Put(Delta<Person> person)
{
var p = person.GetEntity();
Person existingPerson = _repository.Get(p.PersonId);
person.Patch(existingPerson);
_repository.Update();
return;
}
I make the call to the web api like such (using fiddler)
url: http://localhost:49933/api/Person (PUT)
Response Body
{
"PersonId": "b269c49f-8a90-41d6-b102-7cfba3812b1c",
"FirstName": "sample string 2",
"LastName": "sample string 3",
"IsActive": true,
"NumVacationDays": 5,
"Salary": 6.1
}
The controller is hit and al
l the data is populated other than the NumVacationDays (which is 0) and the PersonId (which defaults to 00000000-0000-0000-0000-000000000000)
Does anyone know why the GUIDs and int fields are not populating from the json?
This problem is mentioned in this bug: http://aspnetwebstack.codeplex.com/workitem/562 ...Claims to be fixed but still exists in the 4.0 just released.
The problem being that Newtonsoft Json deserializes a number as Int64 which fails test that IsAssignable to an int, and so it is skipped. Similar issue for guids as strings.
You are supposed to be able to fix this by using the OData media type formatters, which are enabled by deriving from ODataController instead of ApiController. However this had no effect for me - int values still do not work (but when I change the data type to Int64, it works).
I would love to see a working example of posting json with a patch delta that contains an int.
I can venture a guess for what is happening with PersonId but NumVacationDays cannot be explained. My guess is that PersonId is the key property for the Person entity and by default the ODataFormatter does not patch key properties. If you want that behavior, you can change the setting on ODataMediaTypeFormatter.PatchKeyMode to Patch.
Also, it would be interesting to see the value of person.GetChangedPropertyNames() in the action to see if PersonId and NumVacationDays actually show up there or not.