C
C#7mo ago
Ares

How do i solve this type error?

//Create
[HttpPost]
public async Task<IActionResult> Create(AddPollRequest addPollRequest)
{
var result = await HttpContext.AuthenticateAsync(
CookieAuthenticationDefaults.AuthenticationScheme
);


addPollRequest.UserId = await UserInformation.GetGoogleId(result); <-- this line

var domainPoll = mapper.Map<Poll>(addPollRequest);
await pollRepository.CreatePollAsync(domainPoll);
return RedirectToAction("Index", "Home");

}
//Create
[HttpPost]
public async Task<IActionResult> Create(AddPollRequest addPollRequest)
{
var result = await HttpContext.AuthenticateAsync(
CookieAuthenticationDefaults.AuthenticationScheme
);


addPollRequest.UserId = await UserInformation.GetGoogleId(result); <-- this line

var domainPoll = mapper.Map<Poll>(addPollRequest);
await pollRepository.CreatePollAsync(domainPoll);
return RedirectToAction("Index", "Home");

}
why do i get, CS1503 Argument 1: cannot convert from 'Microsoft.AspNetCore.Authentication.AuthenticateResult' to 'Microsoft.Identity.Client.AuthenticationResult
9 Replies
Ares
AresOP7mo ago
This is the userinformation class:
public static class UserInformation
{
public static string GetGoogleId(AuthenticationResult result)
{
if (result.ClaimsPrincipal == null)
throw new Exception("Could not authenticate");

var claims = result
.ClaimsPrincipal.Identities.FirstOrDefault()
?.Claims.Select(claim => new
{
claim.Issuer,
claim.OriginalIssuer,
claim.Type,
claim.Value
})
.ToList();

User newUser;
string? userName = null;
string? userEmail = null;

if

(claims == null || claims.Count == 0)
{
throw new ArgumentOutOfRangeException("Claims count cannot be 0");
}

var googleId = claims
.FirstOrDefault(c =>
c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"
)
.Value;
return googleId;
}
}
public static class UserInformation
{
public static string GetGoogleId(AuthenticationResult result)
{
if (result.ClaimsPrincipal == null)
throw new Exception("Could not authenticate");

var claims = result
.ClaimsPrincipal.Identities.FirstOrDefault()
?.Claims.Select(claim => new
{
claim.Issuer,
claim.OriginalIssuer,
claim.Type,
claim.Value
})
.ToList();

User newUser;
string? userName = null;
string? userEmail = null;

if

(claims == null || claims.Count == 0)
{
throw new ArgumentOutOfRangeException("Claims count cannot be 0");
}

var googleId = claims
.FirstOrDefault(c =>
c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"
)
.Value;
return googleId;
}
}
Keswiik
Keswiik7mo ago
Because you're trying to use two completely different types?
Keswiik
Keswiik7mo ago
AuthenticationHttpContextExtensions.AuthenticateAsync Method (Micro...
Authenticate the current request using the default authentication scheme. The default authentication scheme can be configured using DefaultAuthenticateScheme.
Keswiik
Keswiik7mo ago
While the method you've written is expecting AuthenticationResult from an entirely different namespace
Ares
AresOP7mo ago
i know that but I did using icrosoft.AspNetCore.Authentication.AuthenticateResult at the top of the file and it only gives me the option for Microsoft.Identity.Client.AuthenticationResult
Keswiik
Keswiik7mo ago
public static string GetGoogleId(AuthenticationResult result) fix the type name of your argument here and see if you're able to import the correct namespace
Ares
AresOP7mo ago
let me show you something do you see what i mean? i only have one option
Keswiik
Keswiik7mo ago
Please read the type names I posted earlier. You're not typing the correct one.
Ares
AresOP7mo ago
oh sorry you are right i was ikmprting the wrong one
Want results from more Discord servers?
Add your server