我的机器人应该识别已登录的用户名并显示该用户名。
逻辑已在MessagesController.cs文件中编写并将代码粘贴在以下响应中。.不幸的是,我没有使用VS 2017来获得登录的用户名。感谢您的支持。 refer the screen shot of my code
答案 0 :(得分:0)
using System;
using System.Threading.Tasks;
using System.Web.Http;
using System.DirectoryServices;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Builder.Dialogs;
using System.Web.Http.Description;
using System.Net.Http;
using System.Threading;
using Newtonsoft.Json;
using Microsoft.Bot.Builder.ConnectorEx;
using System.Text;
using System.Linq;
using System.DirectoryServices.AccountManagement;
using System.Security.Principal;
using System.Security.Claims;
using System.Text.RegularExpressions;
namespace Microsoft.Bot.Sample.QnABot
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// receive a message from a user and send replies
/// </summary>
/// <param name="activity"></param>
[ResponseType(typeof(void))]
public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
// check if activity is of type message
if (activity.GetActivityType() == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new RootDialog());
}
else
{
HandleSystemMessage(activity);
}
return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
//var domain = new PrincipalContext(ContextType.Domain);
//var currentUser = UserPrincipal.FindByIdentity(domain, User.Identity.Name);
//Activity reply1 = message.CreateReply("Hi " + currentUser + "");
if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
{
StringBuilder stringb = new StringBuilder();
//string loggedUserName = ClaimsPrincipal.Current.Identity.Name;//WindowsIdentity.GetCurrent().Name.ToString();//System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();//Environment.UserName.ToUpper();
//string loggedUserName = System.Web.HttpContext.Current.User.Identity.Name;
string fullName = System.DirectoryServices.AccountManagement.UserPrincipal.Current.GivenName;
string loggedUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); //WindowsIdentity.GetCurrent().Name.ToString();////Environment.UserName.ToUpper();
string domainUser = Regex.Replace(loggedUserName, ".*\\\\(.*)", "$1", RegexOptions.None);
//string[] words = loggedUserName.Split("\");
stringb.Append("Hi ");
stringb.AppendLine();
stringb.Append("<b>");
stringb.Append("+ fullName ! +");
stringb.Append("</b>");
stringb.AppendLine();
string finalstring = stringb.ToString();
ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
Activity reply1 = message.CreateReply("Hi " + fullName + "");
Activity reply2 = message.CreateReply("This is HR Bot, an I'm your Virtual assistant. How can I help you with your learning today? ");
connector.Conversations.ReplyToActivityAsync(reply1);
connector.Conversations.ReplyToActivityAsync(reply2);
}
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}