Microsoft.AspNetCore.Authentication.Facebook强制重新登录
Microsoft.AspNetCore.Authentication.Facebook force re-login
我已经使用 Microsoft.AspNetCore.Authentication.Facebook nuget 通过 Facebook 社交实现登录。而且我要求每次用户单击登录按钮时,系统都应启动登录过程,并再次询问所有权限,就像是第一次通过 facebook 登录一样。用户也应该能够选择另一个帐户。但是当用户第二次或更多次单击登录按钮时 - 没有出现登录屏幕,我只是获得了 FB 的访问令牌。
我怎样才能再次强制这个登录步骤或者没有这种可能性?也许我应该清除用户浏览器中的 cookie。
我对 aspnetcore 身份验证和浏览器的东西不是很熟悉,因为我是 Xamarin 开发人员
我有:
初始化:
services.AddAuthentication(x =>
{
x.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddFacebook(fb =>
{
fb.AppId = "784137845529334";
fb.AppSecret = "7970214b88892ae601923f852ae79fcd";
fb.SaveTokens = true;
});
控制器:
[HttpGet("LoginOrCreateAccount/{scheme}")]
public async Task LoginOrCreateAccount([FromRoute] string scheme)
{
var auth = await Request.HttpContext.AuthenticateAsync(scheme);
if (!auth.Succeeded
|| auth?.Principal == null
|| !auth.Principal.Identities.Any(id => id.IsAuthenticated)
|| string.IsNullOrEmpty(auth.Properties.GetTokenValue("access_token")))
{
// Not authenticated, challenge
await Request.HttpContext.ChallengeAsync(scheme);
}
else
{
var claims = auth.Principal.Identities.FirstOrDefault()?.Claims;
var email = string.Empty;
email = claims?.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value;
// Get parameters to send back to the callback
var qs = new Dictionary<string, string>
{
{ "access_token", auth.Properties.GetTokenValue("access_token") },
{ "refresh_token", auth.Properties.GetTokenValue("refresh_token") ?? string.Empty },
{ "expires", (auth.Properties.ExpiresUtc?.ToUnixTimeSeconds() ?? -1).ToString() },
{ "email", email }
};
// Build the result url
var url = callbackScheme + "://#" + string.Join("&",
qs.Where(kvp => !string.IsNullOrEmpty(kvp.Value) && kvp.Value != "-1")
.Select(kvp => $"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}"));
// Redirect to final url
Request.HttpContext.Response.Redirect(url);
}
}
通过services.AddAuthentication().AddFacebook()
是不可能达到你的需求的,比如ptompt=login
.
通过找了很多资料,结合你的LoginOrCreateAccount
方法,我推荐你点击按钮的时候,也就是在前端Enabling Re-Authentication
.
Facebook 官方文档: Re-Authentication
我已经使用 Microsoft.AspNetCore.Authentication.Facebook nuget 通过 Facebook 社交实现登录。而且我要求每次用户单击登录按钮时,系统都应启动登录过程,并再次询问所有权限,就像是第一次通过 facebook 登录一样。用户也应该能够选择另一个帐户。但是当用户第二次或更多次单击登录按钮时 - 没有出现登录屏幕,我只是获得了 FB 的访问令牌。 我怎样才能再次强制这个登录步骤或者没有这种可能性?也许我应该清除用户浏览器中的 cookie。 我对 aspnetcore 身份验证和浏览器的东西不是很熟悉,因为我是 Xamarin 开发人员
我有:
初始化:
services.AddAuthentication(x =>
{
x.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddFacebook(fb =>
{
fb.AppId = "784137845529334";
fb.AppSecret = "7970214b88892ae601923f852ae79fcd";
fb.SaveTokens = true;
});
控制器:
[HttpGet("LoginOrCreateAccount/{scheme}")]
public async Task LoginOrCreateAccount([FromRoute] string scheme)
{
var auth = await Request.HttpContext.AuthenticateAsync(scheme);
if (!auth.Succeeded
|| auth?.Principal == null
|| !auth.Principal.Identities.Any(id => id.IsAuthenticated)
|| string.IsNullOrEmpty(auth.Properties.GetTokenValue("access_token")))
{
// Not authenticated, challenge
await Request.HttpContext.ChallengeAsync(scheme);
}
else
{
var claims = auth.Principal.Identities.FirstOrDefault()?.Claims;
var email = string.Empty;
email = claims?.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value;
// Get parameters to send back to the callback
var qs = new Dictionary<string, string>
{
{ "access_token", auth.Properties.GetTokenValue("access_token") },
{ "refresh_token", auth.Properties.GetTokenValue("refresh_token") ?? string.Empty },
{ "expires", (auth.Properties.ExpiresUtc?.ToUnixTimeSeconds() ?? -1).ToString() },
{ "email", email }
};
// Build the result url
var url = callbackScheme + "://#" + string.Join("&",
qs.Where(kvp => !string.IsNullOrEmpty(kvp.Value) && kvp.Value != "-1")
.Select(kvp => $"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}"));
// Redirect to final url
Request.HttpContext.Response.Redirect(url);
}
}
通过services.AddAuthentication().AddFacebook()
是不可能达到你的需求的,比如ptompt=login
.
通过找了很多资料,结合你的LoginOrCreateAccount
方法,我推荐你点击按钮的时候,也就是在前端Enabling Re-Authentication
.
Facebook 官方文档: Re-Authentication