“Hyak.Common.Cloudexception Authentication Failed” 尝试连接到 azure 并加载订阅时

斯拉特

如果在尝试使用我的代码登录 Azure 并对 Azure 进行身份验证时遇到 cloudexception以下 ASP.NET 错误

代码出错的地方

private void LoadSubscriptions()
{
    if (Session["AzureAuthentication"] == null)
        return;

    var mainAuthRes = 
(Task<AuthenticationResult>)Session["AzureAuthentication"];

    var subscriptionCredentials = new TokenCloudCredentials(mainAuthRes.Result.AccessToken);

    var cancelToken = new CancellationToken();
    using (var subscriptionClient = new SubscriptionClient(subscriptionCredentials))
    {
        var tenants = subscriptionClient.Tenants.ListAsync(cancelToken).Result;
        foreach (var tenantDescription in tenants.TenantIds)

我不知道为什么它会在以下位置出现错误:
var tenants = subscriptionClient.Tenants.ListAsync(cancelToken).Result;

mainAuthRes.Result.AccessToken 实际上在 Debug 中显示了一个 Accesstoken

我的授权码

将身份验证作为会话发送到 LoadSubscriptions 类

public void GetAuthorizationCode()
{
    JObject response = new JObject();

    var parameters = new Dictionary<string, string>
        {
            { "response_type", "code" },
            { "client_id", clientId },
            { "redirect_uri", url },
            { "prompt", "login"},
            { "scope", "openid"}
        };

    var requestUrl = string.Format("{0}/authorize?{1}", EndPointUrl,BuildQueryString(parameters));

    Response.Redirect(requestUrl);

}

public string AcquireTokenWithResource(string resource)
{
    var code = Request.Params["code"];
    AuthenticationContext ac = new AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}", directoryId));
    ClientCredential clcred = new ClientCredential(clientId, clientSecret);
        var result = ac.AcquireTokenByAuthorizationCodeAsync(code, new Uri(url), clcred, resource);

    Session["AzureAuthentication"] = result;

    return result.Result.AccessToken;
}

private string BuildQueryString(IDictionary<string, string> parameters)
{
    var list = new List<string>();

    foreach (var parameter in parameters)
    {
        list.Add(string.Format("{0}={1}", parameter.Key, HttpUtility.UrlEncode(parameter.Value)));
    }

    return string.Join("&", list);
}

protected string EndPointUrl
{
    get
    {
        return string.Format("{0}/{1}/{2}", "https://login.microsoftonline.com", directoryId, @"oauth2/");
    }
}

谁能看出哪里出错了。

如果您需要更多详细信息,请询问我。

斯拉特

我最终使用了一种不同的方式来验证自己,因为我相信这种验证方式会不断给我带来错误,即使它最终会奏效。这就是为什么我要关闭这个帖子。

我不会推荐我的新身份验证方法,因为它涉及一个自创建的登录页面,该页面使用在线发现的加密器发送凭据,该加密器通过会话或配置字段或凭据的硬编码字段加密。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我应该捕捉哪个:Hyak.Common.CloudException或Microsoft.WindowsAzure.CloudException?

尝试连接到 Azure 中的 API 但允许来源时出错

将 JMeter 连接到 MongoDb 时出现问题:com.mongodb.CommandFailureException ... Authentication failed

“ CloudException:...对提供的存储帐户的读取或写入权限不足。” 在Azure中启用DBServer审核日志时

尝试连接到蓝牙扬声器时发生错误:org.bluez.Error.Failed`

SSL:CERTIFICATE_VERIFY_FAILED 尝试连接到 python3.7 中的 websocket 时

尝试通过 AJP 将非 Docker Apache 连接到 Docker Tomcat 时出现“ajp_ilink_receive failed”

如何使用连接令牌连接到Azure订阅?

尝试连接到Azure SQL服务器时,我得到PDOException找不到驱动程序

尝试使用WebAPI从Windows Phone 8应用连接到ms-azure上的MVC服务器时出错

尝试连接到Networkshare时句柄无效

尝试连接到RabbitMQ时出现IncompatibleProtocolError

尝试连接到 vTiger 时凭据无效

尝试从 quarkus 连接到 keycloak 时出错

CloudException:找不到合适的云连接器

尝试让两个 Azure VM 在不同端口上进行通信时出现“无法连接到远程主机:连接被拒绝”错误

Golang:尝试连接到 sql server(不同主机)时出现“Err TLS Handshake failed: tls: server selected unsupported protocol version 301”

尝试连接到Azure Web App的授权错误

使用CF CLI连接到PCF Dev时出现“ FAILED”

尝试连接到蓝牙服务器时连接被拒绝

尝试连接到服务器时,Sshutle连接被拒绝

尝试连接到Kubernetes中的服务时连接被拒绝

Get-AzRoleAssignment 在 Azure Runbook 中引发 Microsoft.Rest.Azure.CloudException

加载从 android studio 连接到 azure 门户的连字符时出错

尝试连接调试器时,Azure Functions崩溃,导致nodemon永久重新加载

设置连接到Azure的实体框架时超时

连接到SQL Server.PDOException对象时出错(Azure)

使用Cassandra连接到Azure Cosmos DB时出现UnknownHostException

Azure-从部署源连接到VSTS时出错