使用Owin cookie身份验证时遇到一个奇怪的问题.
当我启动我的IIS服务器时,身份验证在IE/Firefox和Chrome上运行得非常好.
我开始使用身份验证进行一些测试并在不同的平台上登录,我想出了一个奇怪的错误.偶尔Owin框架/ IIS只是不向浏览器发送任何cookie.我将输入一个用户名和密码,该代码运行正确,但根本没有cookie传递给浏览器.如果我重新启动服务器它开始工作,那么在某些时候我将尝试登录并再次cookie停止交付.单步执行代码不会做任何事情并且不会抛出任何错误.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
CookieHttpOnly = true,
AuthenticationType = "ABC",
LoginPath = new PathString("/Account/Login"),
CookiePath = "/",
CookieName = "ABC",
Provider = new CookieAuthenticationProvider
{
OnApplyRedirect = ctx =>
{
if (!IsAjaxRequest(ctx.Request))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
在我的登录程序中,我有以下代码:
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var authentication = HttpContext.Current.GetOwinContext().Authentication;
var identity = new ClaimsIdentity("ABC");
identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.User_ID.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Role, role.myRole.ToString()));
authentication.AuthenticationResponseGrant =
new AuthenticationResponseGrant(identity, new AuthenticationProperties()
{
IsPersistent = isPersistent
}); …Run Code Online (Sandbox Code Playgroud) 我创建了一个新的MVC5 Web应用程序,当我尝试使用Google或Facebook登录时,调用了ExternalLoginCallbackAction AccountController,但GetExternalLoginInfoAsync()始终返回null:
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
Run Code Online (Sandbox Code Playgroud)
因为它总是为null,所以它只是重定向回登录页面并且过程重新开始.我怎样才能解决这个问题?
一切都习以为常,直到fb将api升级到2.4 (我以前的项目中有2.3).
今天当我在fb开发人员上添加一个新的应用程序时,我得到了api 2.4.
问题:现在我从fb(loginInfo.email = null)收到空电子邮件.
当然,我检查了用户电子邮件在fb配置文件中处于公共状态,
我去了loginInfo对象,但没有找到任何其他电子邮件地址.
我谷歌,但没有找到任何答案.
请任何帮助..我有点迷路..
谢谢,
我的原始代码(适用于2.3 api):
在AccountController.cs中:
//
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
//A way to get fb details about the log-in user:
//var firstNameClaim = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:first_name"); <--worked only on 2.3
//var firstNameClaim = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:name"); <--works on 2.4 api …Run Code Online (Sandbox Code Playgroud) c# facebook facebook-graph-api asp.net-mvc-5 asp.net-identity-2
我有ASP.NET 5项目,我使用Web API建立外部登录(对于Facebook和谷歌).在我的例子中,我有Web API控制器(非MVC控制器),其中包含以下代码:
[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
if (error != null)
return Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error));
if (!User.Identity.IsAuthenticated)
return new ChallengeResult(provider, this);
var exLog = await Authentication.GetExternalLoginInfoAsync();
Run Code Online (Sandbox Code Playgroud)
当移动应用程序调用此方法时,我正在获得正确的身份验证请求并且属性User.Identity.IsAuthenticated为true,我可以在调试器中看到用户是正确的,但问题是exlog变量始终为null.
这是Authentication物业
private IAuthenticationManager Authentication => this.Request.GetOwinContext().Authentication;
Run Code Online (Sandbox Code Playgroud)
我在堆栈上阅读了关于这个bug的每个问题,但没有任何帮助,大多数问题都是针对MVC控制器的,所以这没有帮助.
例如,这个问题对我没有帮助,因为它适用于MVC.
更新 我正在使用Owin 4.0