2017-03-21 11 views
-1

私の要件では、Facebookログインボタンを追加して、ユーザーがFacebook資格情報で認証できるようにする必要があります。ログインページのFacebookログインボタンをC#mvc4に追加します。

私は多くのリンクをGoogleで検索しかし、私が見つけた私の要件はありませんMVCが提供するデフォルトのログインページに、私のログインページにFacebookのボタンを追加し、何であるか

https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

を下回っています。

は、私は1つ便利なものを見つけましたが、そのは例外 PFB私は私はそれを解決することはできませんよのActionResult Facebookの()内のいくつかのエラーを取得しています

public class AccountController : Controller 
    { 
     private Uri RedirectUri 
     { 
      get 
      { 
       var uriBuilder = new UriBuilder(Request.Url); 
       uriBuilder.Query = null; 
       uriBuilder.Fragment = null; 
       uriBuilder.Path = Url.Action("FacebookCallback"); 
       return uriBuilder.Uri; 
      } 
     } 

     [AllowAnonymous] 
     public ActionResult login() 
     { 
      return View(); 
     } 

     public ActionResult logout() 
     { 
      FormsAuthentication.SignOut(); 
      return View("Login"); 
     } 

     [AllowAnonymous] 
     public ActionResult Facebook() 
     { 
      var fb = new FacebookClient(); 
      var loginUrl = fb.GetLoginUrl(new 
      { 
       client_id = "444195149059600", 
       client_secret = "89223ca2d87cc4a741000d5c1ea57694", 
       redirect_uri = RedirectUri.AbsoluteUri, 
       response_type = "code", 
       scope = "email" // Add other permissions as needed 
      }); 

      return Redirect(loginUrl.AbsoluteUri); 
     } 

     public ActionResult FacebookCallback(string code) 
     { 
      var fb = new FacebookClient(); 
      dynamic result = fb.Post("oauth/access_token", new 
      { 
       client_id = "444195149059600", 
       client_secret = "89223ca2d87cc4a741000d5c1ea57694", 
       redirect_uri = RedirectUri.AbsoluteUri, 
       code = code 
      }); 

      var accessToken = result.access_token; 

      // Store the access token in the session for farther use 
      Session["AccessToken"] = accessToken; 

      // update the facebook client with the access token so 
      // we can make requests on behalf of the user 
      fb.AccessToken = accessToken; 

      // Get the user's information 
      dynamic me = fb.Get("me?fields=first_name,middle_name,last_name,id,email"); 
      string email = me.email; 
      string firstname = me.first_name; 
      string middlename = me.middle_name; 
      string lastname = me.last_name; 

      // Set the auth cookie 
      FormsAuthentication.SetAuthCookie(email, false); 
      return RedirectToAction("Index", "Home"); 
     } 
    } 
} 

を参照のうえ午前のコードを投げます。 同じことを教えてもらえますか?高度な

+1

ショーが詳細にそこに遭遇したようなので、お使いの外部ログインで

 public partial class Startup { public static IDataProtectionProvider DataProtectionProvider { get; set; } public void ConfigureAuth(IAppBuilder app) { app.UseFacebookAuthentication( appId: "", appSecret: ""); } } 

スタートアップクラスには、メソッドを持っています。 –

答えて

0

で おかげであなたのリンクは、MVC 5を参照して、あなたがMVC 4を使用する場合はここになります。https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/using-oauth-providers-with-mvc

これらのバージョンの間にいくつかの重要な違いがあります...そして、あなたが受け取っに関する詳細な情報を提供する場合エラーは、

のBstは、あなたがこのようにしていなければなりませんあなたのビューで

+0

var fb = new FacebookClient();のエラーが発生しました。 VAR loginUrl = fb.GetLoginUrl(新しい { のclient_id = "444195149059600"、 client_secret = "89223ca2d87cc4a741000d5c1ea57694"、 REDIRECT_URI = RedirectUri.AbsoluteUri、 response_type = "コード"、 スコープ= "メール" //他のパーミッションを追加します。必要に応じて });私はFacebookClientクラスからGetLoginUrlを取得できません –

+0

おそらくyorの文字列 'dynamic me'は正しくありませんか? https://gist.github.com/ntotten/4341111と比較すると、この実装ではクライアントemaiのみが使用されます。 もう1つのこと:この質問では、クライアントIDとクライアント秘密を匿名で指定する必要があります。これらが "本当の"価値観のように見える... – Joshit

0

をrgrds根本的な問題についての具体的なヒントを与えることが容易です。

using (Html.BeginForm("ExternalLogin", "Account", new {Model.ReturnUrl})) 
     { 
      @Html.AntiForgeryToken() 
      <div id="socialLoginList"> 
       <p> 
        @foreach (var p in loginProviders) 
        { 
         <button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button> 
        } 
       </p> 
      </div> 
     } 

あなたのアプリケーションにOWINをインストールすると仮定しました。行がエラーをスローし、どのような例外(S)この

[AllowAnonymous] 
      public async Task<ActionResult> VerifyCode(string provider, string returnUrl, bool rememberMe) 
      { 
       // Require that the user has already logged in via username/password or external login 
       if (!await SignInManager.HasBeenVerifiedAsync()) 
        return View("Error"); 
       return View(new VerifyCodeViewModel { Provider = provider, ReturnUrl = returnUrl, RememberMe = rememberMe }); 
      } 

[AllowAnonymous] 
     public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
     { 
      var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
      if (loginInfo == null) 
       return RedirectToAction("Login"); 

      // Sign in the user with this external login provider if the user already has a login 
      var result = await SignInManager.ExternalSignInAsync(loginInfo, false); 
      switch (result) 
      { 
       case SignInStatus.Success: 
        return RedirectToLocal(returnUrl); 
       case SignInStatus.LockedOut: 
        return View("Lockout"); 
       case SignInStatus.RequiresVerification: 
        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }); 
       case SignInStatus.Failure: 
       default: 
        // If the user does not have an account, then prompt the user to create an account 
        ViewBag.ReturnUrl = returnUrl; 
        ViewBag.LoginProvider = loginInfo.Login.LoginProvider; 
        return View("ExternalLoginConfirmation", 
         new ExternalLoginConfirmationViewModel { Email = loginInfo.Email }); 
      } 
     } 

    // 
    // POST: /Account/ExternalLoginConfirmation 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, 
     string returnUrl) 
    { 
     if (User.Identity.IsAuthenticated) 
      return RedirectToAction("Index", "Manage"); 

     if (ModelState.IsValid) 
     { 
      // Get the information about the user from the external login provider 
      var info = await AuthenticationManager.GetExternalLoginInfoAsync(); 
      if (info == null) 
       return View("ExternalLoginFailure"); 
      var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
      var result = await UserManager.CreateAsync(user); 
      if (result.Succeeded) 
      { 
       result = await UserManager.AddLoginAsync(user.Id, info.Login); 
       if (result.Succeeded) 
       { 
        await SignInManager.SignInAsync(user, false, false); 
        return RedirectToLocal(returnUrl); 
       } 
      } 
      AddErrors(result); 
     } 

     ViewBag.ReturnUrl = returnUrl; 
     return View(model); 
    } 
関連する問題