示例#1
0
        public ActionResult Authenticate(string returnUrl)
        {
            // handle oauth authentication
            if (string.IsNullOrEmpty(Request.Form["openid_identifier"]))
            {
                // handle oauth version 2.0
                if (Request.Form["oauth_version"] == "2.0")
                {
                    return(FacebookAuth(returnUrl));
                }
            }

            var response = openid.GetResponse();

            if (response == null)
            {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
                {
                    try
                    {
                        var request = openid.CreateRequest(Request.Form["openid_identifier"]);
                        var fetch   = new FetchRequest();
                        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                        request.AddExtension(fetch);

                        return(request.RedirectingResponse.AsActionResult());
                    }
                    catch (ProtocolException ex)
                    {
                        ViewData["Message"] = ex.Message;
                        return(View("Logon"));
                    }
                }
                else
                {
                    ViewData["Message"] = "Invalid identifier";
                    return(View("Logon"));
                }
            }
            else
            {
                // Stage 3: OpenID Provider sending assertion response
                switch (response.Status)
                {
                case AuthenticationStatus.Authenticated:

                    UsersModels user = new UsersModels();

                    var    fetch = response.GetExtension <FetchResponse>();
                    string email = null;
                    if (fetch != null)
                    {
                        email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                    }

                    // for new OpenId
                    user = user.GetUserByOpenId(response.ClaimedIdentifier);
                    if (user == null)
                    {
                        RegisterOpenId roi = new RegisterOpenId();
                        roi.ClaimedOpenId  = response.ClaimedIdentifier;
                        roi.FriendlyOpenId = PostHelper.GetFriendlyOpenId(response, email);
                        roi.ReturnUrl      = returnUrl;
                        roi.Email          = email;
                        return(View(roi));
                    }

                    FormsAuthenticationService formAuth = new FormsAuthenticationService();
                    formAuth.SignIn(response.ClaimedIdentifier, false);

                    if (!string.IsNullOrEmpty(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                case AuthenticationStatus.Canceled:
                    ViewData["Message"] = "Canceled at provider";
                    return(View("Logon"));

                case AuthenticationStatus.Failed:
                    ViewData["Message"] = response.Exception.Message;
                    return(View("Logon"));
                }
            }
            return(new EmptyResult());
        }