Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不使用登录名和密码登录api团队_C#_.net_Microsoft Teams_Webapi_Microsoft Graph Teams - Fatal编程技术网

C# 不使用登录名和密码登录api团队

C# 不使用登录名和密码登录api团队,c#,.net,microsoft-teams,webapi,microsoft-graph-teams,C#,.net,Microsoft Teams,Webapi,Microsoft Graph Teams,我的代码有问题。我想这样做登录窗口(如图所示)只在我登录时打开一次。 下一次,当我启动程序时,登录名和密码将被记住。 我的程序中有什么变化使得这个登录窗口不会弹出?或者如何进行域登录? 这是我的密码 private static async System.Threading.Tasks.Task DodajSpotkanieDoTeamsAsync(Spotkanie-Spotkanie) { AuthenticationResult authResult=null; var app=app

我的代码有问题。我想这样做登录窗口(如图所示)只在我登录时打开一次。 下一次,当我启动程序时,登录名和密码将被记住。 我的程序中有什么变化使得这个登录窗口不会弹出?或者如何进行域登录?
这是我的密码

private static async System.Threading.Tasks.Task DodajSpotkanieDoTeamsAsync(Spotkanie-Spotkanie)
{
AuthenticationResult authResult=null;
var app=app.PublicClientApp;
var accounts=wait app.GetAccountsAsync();
var firstAccount=accounts.FirstOrDefault();
尝试
{
authResult=await app.AcquireTokenSilent(作用域,firstAccount)
.ExecuteAsync();
}
捕获(MsalUiRequiredException ex)
{
尝试
{
authResult=await app.AcquireTokenInteractive(作用域)
.WithAccount(accounts.FirstOrDefault())
.ExecuteAsync();
}
捕获(MsalException-msalex)
{
}
}
捕获(例外情况除外)
{
返回;
}
if(authResult!=null)
{
GraphServiceClient graphClient=新GraphServiceClient(
"https://graph.microsoft.com/v1.0",
新的DelegateAuthenticationProvider(
异步(重新请求消息)=>
{
requesMessage.Headers.Authorization=新系统.Net.Http.Headers.AuthenticationHeaderValue(“承载者”,authResult.AccessToken);
}));
var@event=新事件
{
主题=spotkanie.Tytul,
Body=新项目Body
{
ContentType=BodyType.Html,
},
开始=新日期时区
{
DateTime=spotkanie.DataRozpoczecia,
时区=“太平洋标准时间”
},
End=新日期时区
{
DateTime=spotkanie.DataZakonczenia,
时区=“太平洋标准时间”
},
IsOnlineMeeting=true,
OnlineMeetingProvider=OnlineMeetingProviderType.TeamsForBusiness
};
var listaGosci=新列表();
foreach(spotkanie.ListaGosci.Split(“|”)中的变量gosc)
{
listaGosci.Add(
新与会者
{
EmailAddress=新的EmailAddress
{
地址=gosc
},
}
);
}
@event.attendes=listaGosci;
尝试
{
var request=wait graphClient.Me.Calendar.Events
.Request()
.AddAsync(@event);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
}

您可以构建自己的登录表单,缓存数据(登录和pw),并在REST调用中使用它来获取令牌,而不是使用内置函数获取令牌:

var client = new RestClient("https://login.microsoftonline.com/" + domainname);
var request = new RestRequest("/oauth2/token", Method.POST);   request.AddBody("grant_type", "client_credentials");
request.AddParameter("client_id", clientId);
request.AddParameter("client_secret", clientSecret);
request.AddParameter("Resource", "https://graph.microsoft.com");
request.AddParameter("scope", "[scopes]"); 
IRestResponse response = client.Execute(request);

var token= response.Content;
要安全地存储凭据,请使用Windows凭据管理:

{   
public static UserPass GetCredential(string target)
    {
        var cm = new Credential {Target = target};
        if (!cm.Load())
        {
            return null;
        }
        return new UserPass(cm.Username, cm.Password);
    }
    
    public static bool SetCredentials(string target, string username, string password, PersistanceType persistenceType)
    {
        return new Credential {Target = target,
                Username = username,
                Password = password,
                PersistanceType =  persistenceType}.Save();
    }
    
    public static bool RemoveCredentials(string target)
    {
        return new Credential { Target = target }.Delete();
    }
}


CredentialUtil.SetCredentials("FOO", "john", "1234", PersistanceType.LocalComputer);
var userpass = CredentialUtil.GetCredential("FOO");
Console.WriteLine($"User: {userpass.Username} Password: {userpass.Password}");
CredentialUtil.RemoveCredentials("FOO");
Debug.Assert(CredentialUtil.GetCredential("FOO") == null);
对于.net PM命令:

Install-Package CredentialManagement -Version 1.0.2

请发布代码,而不是截图OK,已经编辑:)@Piotr Platos:下面的代码解决了您的问题吗?我使用了不同的方法。我从azure(quickstart)下载了ready代码@Piotr:您仍然面临问题吗?
Install-Package CredentialManagement -Version 1.0.2