我正在努力配置服务器端Web应用程序以使用外部登录提供程序(在这种情况下为Discord)登录。我正在为我的oauth提供者使用https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers。值得注意的是,此应用程序仅用于 使用不和谐的外部提供程序,没有使用用户名/密码的常规登录功能。
这是我在Startup.cs的ConfigureServices方法中用于设置身份验证,授权和不一致的oauth中间件的内容:
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.User.RequireUniqueEmail = false;
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequiredUniqueChars = 0;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Lockout.AllowedForNewUsers = false;
}).AddEntityFrameworkStores<ApplicationDbContext>();
services.AddAuthorization();
services.AddAuthentication().AddDiscord(x =>
{
x.ClientId = Configuration["AppId"];
x.ClientSecret = Configuration["AppSecret"];
x.Scope.Add("guilds");
x.Scope.Add("identify");
x.Validate();
});
这就是我的Configure方法中的
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// environment stuff
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
无论出于何种原因,无论何时执行挑战(成功重定向到不和谐的登录,然后重定向回/ sigin-discord),它都不会登录用户。
这是我的身份控制器,用于处理登录,挑战发生后,将用户发送到/ sigin-discord,然后重定向到/ sigin,以便登录用户或为他们创建帐户(如果他们还没有。
[HttpGet("login")]
public async Task login(string redirectUrl)
{
string redirectUri = Url.Action("ExternalLoginCallback","Identity",new { returnUrl = redirectUrl});
var properties = await signInManager.GetExternalAuthenticationSchemesAsync();
await HttpContext.ChallengeAsync(properties.FirstOrDefault().Name, new AuthenticationProperties { RedirectUri = redirectUri });
}
[HttpGet("signin")]
public async Task ExternalLoginCallback(string returnUrl)
{
returnUrl = returnUrl ?? "/";
var info = await signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
HttpContext.Response.Redirect(returnUrl);
}
var result = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, true);
if (result.Succeeded)
{
HttpContext.Response.Redirect(returnUrl);
}
else
{
var username = info.Principal.FindFirstValue(ClaimTypes.Name);
if (username != null)
{
var user = await UserManager.FindByNameAsync(info.Principal.Identity.Name);
if (user == null)
{
user = new ApplicationUser(info.Principal.FindFirstValue(ClaimTypes.Name), Convert.ToUInt64(info.ProviderKey));
await UserManager.CreateAsync(user);
}
await UserManager.AddLoginAsync(user, info);
await signInManager.SignInAsync(user, true);
}
HttpContext.Response.Redirect(returnUrl);
}
但是。即使我成功完成了挑战,signInManager.GetExternalLoginInfoAsync()始终返回null。
有人尝试使用blazor来使用外部登录提供程序吗?如果是这样,有谁知道为什么它不起作用?