就像标题一样,我在使用脸书和身份时遇到问题。我使用Microsoft文档来为我的项目实现身份和Facebook授权。我可以用身份登录,但是当我尝试用Facebook登录时,没有任何反应。我的客户收到Cookie(“ identity.external”),但是“查看”不会查看该用户是否在线。
在View中,我有方法:
@if (User.Identity.IsAuthenticated)
我用于Facebook登录的控制器方法
public IActionResult SignIn(String provider)
{
return Challenge(new Microsoft.AspNetCore.Authentication.AuthenticationProperties { RedirectUri = "/" }, provider);
}
我的启动配置
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContextPool<StoreContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("StoreDatabase")));
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<StoreContext>();
services.AddMemoryCache();
//session
services.AddDistributedMemoryCache();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSession(options =>
{
options.Cookie.Name = ".MyName";
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
// Make the session cookie essential
options.Cookie.IsEssential = true;
});
services.AddHttpContextAccessor();
//session
services.AddAuthentication(options =>
{
//options.DefaultChallengeScheme = FacebookDefaults.AuthenticationScheme;
//options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
//options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddFacebook(options =>
{
options.AppId = "CODE";
options.AppSecret = "CODE";
}).AddCookie();
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}
).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
当我取消评论
//options.DefaultChallengeScheme = FacebookDefaults.AuthenticationScheme;
//options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
//options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
然后,facebook有效,但身份无效。
谢谢!
答案 0 :(得分:0)
此facebook身份验证非常复杂。 我将其余的控制器方法发布给其他人。
public async Task<IActionResult> ExternalLoginCallback(string returnUrl)
{
ExternalLoginInfo info = await signInManager.GetExternalLoginInfoAsync();
var result = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
if (result.Succeeded)
{
return RedirectToLocal(returnUrl);
}
else
{
var user = new ApplicationUser
{
UserName = info.Principal.FindFirstValue(ClaimTypes.Email),
Email = info.Principal.FindFirstValue(ClaimTypes.Email),
UserData = new UserData { Email = info.Principal.FindFirstValue(ClaimTypes.Email) }
};
var registrationResult = await userManager.CreateAsync(user);
if (registrationResult.Succeeded)
{
registrationResult = await userManager.AddLoginAsync(user, info);
if (registrationResult.Succeeded)
{
await signInManager.SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
else
throw new Exception("trut Błąd! External provider assocation error");
}
else
throw new Exception("trut błąd! Registration error");
}
}
和登录方法
public IActionResult SignIn(string provider, string returnUrl)
{
var properties = signInManager.ConfigureExternalAuthenticationProperties("Facebook", Url.Action("ExternalLoginCallback", "Account", new { returnUrl = returnUrl}));
return Challenge(properties, "Facebook");
}