【问题标题】:Is this basic OAuth Owin authentication process in MVC.net is safe enough (with Google and Facebook)MVC.net 中的这个基本 OAuth Owin 身份验证过程是否足够安全(使用 Google 和 Facebook)
【发布时间】:2018-03-14 06:19:45
【问题描述】:

我实现了一个基本的 owin OAuth 身份验证过程(使用 Google 和 Facebook),但我有点担心外部 LoginCallback 中的“登录信息”是否足够安全以供使用,我是否必须做任何事情别的 ?这里是“结束”AccountController 代码,但主要内容是在遵循此模式/示例时完成的:

https://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application#MinimalCodeSummary

谢谢!

private const string XsrfKey = "XsrfId";
internal class ChallengeResult : HttpUnauthorizedResult
{
    public ChallengeResult(string provider, string redirectUri)
        : this(provider, redirectUri, null)
    {
    }

    public ChallengeResult(string provider, string redirectUri, string userId)
    {
        LoginProvider = provider;
        RedirectUri = redirectUri;
        UserId = userId;
    }

    public string LoginProvider { get; set; }
    public string RedirectUri { get; set; }
    public string UserId { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
        if (UserId != null)
        {
            properties.Dictionary[XsrfKey] = UserId;
        }
        context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
    }
}

[HttpPost]
[AllowAnonymous]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    ChallengeResult r = new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Users", new { ReturnUrl = returnUrl }));
    return r;
}

[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    if (returnUrl == null) returnUrl = string.Empty;  // just to reduce validation below...

    // Get login info
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    if (loginInfo == null)
    {
        // if none, return to login page
        return RedirectToAction("Login");
    }
    else
    {
        // is this login info safe and thrustable to create or login the user
        string username = loginInfo.Login.LoginProvider + "_ " + loginInfo.Login.ProviderKey;
    }
}

【问题讨论】:

    标签: asp.net-mvc oauth owin


    【解决方案1】:

    Google 和 Facebook 实施的身份验证方法非常安全,但您必须记住,您的 ExternalLoginExternalLoginCallback 允许匿名调用,这意味着任何人都可以调用它们。

    因此,检查ExternalLogin 上的 AntiForgery 令牌可能是个好主意。用[ValidateAntiForgeryToken] 装饰它。查看更多here

    在您的ExternalLoginCallback 上,您可以添加验证以检查请求是否来自已知网址,例如 Google 或 Facebook 的网址。如果不是,则引发错误。

    当然,在生产环境中总是使用 https。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-02
      • 2018-11-19
      • 2013-03-02
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      相关资源
      最近更新 更多