如何通过OpenId认证登录user



你必须原谅我不知道OpenId背后的魔力。在我的流动中;用户从外部站点定向并使用OpenId进行身份验证。它们被返回到我的应用程序以创建额外的用户名和密码。他们在我的应用程序上注册了他们的帐户并登录了。

当他们在注册后(外部网站)从他们的网站启动我的应用程序时,我需要让用户自动登录,尊重OpenId凭据。

我使用下面的代码:

    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[XSRF_KEY] = UserId;
            }
            context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
        }

然后重定向到此函数:

        var openid = new OpenIdRelyingParty();
        var response = openid.GetResponse();
        if (response == null)
        {
            Identifier id;
            if (Identifier.TryParse(_identifer, out id))
            {
                try
                {
                    return openid.CreateRequest(_identifer).RedirectingResponse.AsActionResultMvc5();
                } catch (ProtocolException e)
                {
                    throw new Exception(e.Message);
                }
            }
            return View("Login");
        }
        switch (response.Status)
        {
            case AuthenticationStatus.Authenticated:
                Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
                FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false);
                // Now I need to log in the user
                break;
            case AuthenticationStatus.Canceled:
               break;
            case AuthenticationStatus.Failed:
               break;
        }

OpenIdRelyingParty返回响应并通过认证后,我需要登录用户。我是否需要启用表单身份验证才能使其工作?我使用MVC Identity 2.0

这是我在public ActionResult ExternalLoginCallback(string returnUrl)方法中的一部分,这是从外部OpenId服务器调用的方法(在这种情况下是Intuit,但它不应该重要)。

//Attempt to log the user in if they've already created an account with the external login.
if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
{        
    return RedirectToLocal(returnUrl);
}

相关内容

最新更新