gpt4 book ai didi

c# - 对现有 ASP.NET Core Identity 应用程序进行 Azure AD 身份验证

转载 作者:行者123 更新时间:2023-12-05 05:36:53 26 4
gpt4 key购买 nike

我目前有一个使用身份来授权用户的应用程序。我需要将其更改为使用 Azure AD 登录。通过 azure 进行身份验证后,我需要使用身份数据库中的登录用户信息。用户通过身份验证后,我得到一个NullReferenceException:未将对象引用设置为对象的实例。并在此时失败:ApplicationUser 用户=等待manager.FindByNameAsync(context.Principal.Identity.Name);

enter image description here

```
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication().AddOpenIdConnect(c =>
{
c.Authority = "https://login.microsoftonline.com/common";
c.ClientId = "<insert-registered-guid>";
c.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false
};
c.Events.OnTokenValidated = async context =>
{
UserManager<ApplicationUser> manager = context.HttpContext.RequestServices.GetService<UserManager<ApplicationUser>>();
SignInManager<ApplicationUser> signIn = context.HttpContext.RequestServices.GetService<SignInManager<ApplicationUser>>();
ApplicationUser user = await manager.FindByNameAsync(context.Principal.Identity.Name);
if (user != null)
{
await signIn.SignInAsync(user, false);
}
};
});
}

// HomeController.cs
using Microsoft.AspNetCore.Authentication.OpenIdConnect;

public class HomeController : Controller
{
[AllowAnonymous]
public IActionResult LoginWithAzure()
{
string redirectUrl = Url.Content("~/");
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl }, OpenIdConnectDefaults.AuthenticationScheme);
}
}
```

更新:

我能够克服错误,因为我失踪了

services.AddIdentity

现在的问题是它陷入了 OnTokenValidated 内的循环中。

 UserManager<ApplicationUser> manager = context.HttpContext.RequestServices.GetService<UserManager<ApplicationUser>>();
SignInManager<ApplicationUser> signIn = context.HttpContext.RequestServices.GetService<SignInManager<ApplicationUser>>();
ApplicationUser user = await manager.FindByNameAsync(context.Principal.Identity.Name);




if (user != null)
{
await signIn.SignInAsync(user, false);
}

在 if 语句之后,它返回到经理行。

最佳答案

上述解决方案不起作用,因此我更改了它。

Startup.cs 更改为以下内容:

        //  Add Azure AD authentication
services.AddAuthentication(defaultScheme: AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));

AccountController.cs 更改为:

 [AllowAnonymous]
[HttpGet]
public ChallengeResult InternalSignIn(string returnUrl = "/")
{
var redirectUrl = Url.Action(nameof(ExternalLoginCallback));
var properties = signInManager.ConfigureExternalAuthenticationProperties(AzureADDefaults.AuthenticationScheme, redirectUrl);
return new ChallengeResult(AzureADDefaults.AuthenticationScheme, properties);
}



[HttpGet]
public async Task<IActionResult> ExternalLoginCallback()
{
var info = await signInManager.GetExternalLoginInfoAsync();


if (info is null)
{

return BadRequest();
}

var signInResult = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: false);


var email = info.Principal.FindFirstValue(ClaimTypes.Name);


var user = await userManager.FindByEmailAsync(email);
IdentityResult result;
if (user != null)
{
var logins = await userManager.GetLoginsAsync(user);
if (!logins.Any())
{
result = await userManager.AddLoginAsync(user, info);
if (!result.Succeeded)
{

return View();
}
}

await signInManager.SignInAsync(user, isPersistent: false);
return RedirectToAction(nameof(HomeController.Index),"Home");
}


return StatusCode(500, "Internal server error");
}

关于c# - 对现有 ASP.NET Core Identity 应用程序进行 Azure AD 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73227205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com