I’m using the Microsoft Entra ID to login in my .net 4.8 application, but, if I try to read (using VS watch), I received an sql server error. I also tried to save the type and the value of my claims in a variable, but when the VS try to read the last claim, the error appear again.
It’s doesn’t have sense for me, because I’m just trying to read a claim!
The error is: System.Web.HttpException:
‘Unable to connect to SQL Server database.‘,
and the inner exception is: SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)
Here is my code, my error appears on foreach line.
if (HttpContext.User is ClaimsPrincipal claimsPrincipal && claimsPrincipal.Identity.IsAuthenticated)
{
var email = ClaimsManager.GetClaimValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name");
var IsSuccess = false;
var claimsInfo = new StringBuilder();
foreach (var claim in claimsPrincipal.Claims)
{
claimsInfo.AppendLine($"Type: {claim.Type}, Value: {claim.Value}");
var temp = claimsInfo.ToString();
}
var claimsString = claimsInfo.ToString();
...
}
Additional information:
Startup:
public void Configuration(IAppBuilder app)
{
// Configuração de autenticação por cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ApplicationCookie",
LoginPath = new PathString("/Access/Login"),
LogoutPath = new PathString("/Access/Logout"),
CookieName = "ApplicationCookie"
});
// Configurar UniqueClaimTypeIdentifier
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = ConfigurationManager.AppSettings["AzureAd:ClientId"],
Authority = $"{ConfigurationManager.AppSettings["AzureAd:Instance"]}{ConfigurationManager.AppSettings["AzureAd:TenantId"]}",
ClientSecret = ConfigurationManager.AppSettings["AzureAd:ClientSecret"],
RedirectUri = ConfigurationManager.AppSettings["AzureAd:RedirectUri"],
PostLogoutRedirectUri = ConfigurationManager.AppSettings["AzureAd:PostLogoutRedirectUri"],
SignInAsAuthenticationType = "ApplicationCookie",
Scope = "openid profile email User.Read",
ResponseType = "code id_token",
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error/Errors?message=" + context.Exception.Message);
return Task.CompletedTask;
},
AuthorizationCodeReceived = context => {
var clientId = ConfigurationManager.AppSettings["AzureAd:ClientId"];
var clientSecret = ConfigurationManager.AppSettings["AzureAd:ClientSecret"];
var tenantId = ConfigurationManager.AppSettings["AzureAd:TenantId"];
// Obter o ID do usuário logado a partir do token
var userId = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
if (string.IsNullOrEmpty(userId))
{
// Handle the case where userId is null or empty
context.Response.Redirect("/Error/Errors?message=User ID not found");
return Task.CompletedTask;
}
return Task.CompletedTask;
},
SecurityTokenValidated = context =>
{
return Task.CompletedTask;
}
}
});
And the method that calls azure to obtain the claims:
public void AzureAD()
{
try
{
HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
// Desafiar a autenticação
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = ConfigurationManager.AppSettings["AzureAd:RedirectUri"] },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
catch (Exception)
{
throw;
}
}
I just wanna understand why my code is trying to open connection with SQL if I’m just trying to read a claim. How can I resolve this?