如何在ASP.NET MVC网站上登录面书 [英] How can login with face book in ASP.NET MVC website

查看:75
本文介绍了如何在ASP.NET MVC网站上登录面书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[AllowAnonymous]
public ActionResult Facebook()
{
    var fb = new FacebookClient();
    var loginUrl = fb.GetLoginUrl(new
    {
        client_id = "xxxxxx",
        client_secret = "xxxxx",
        redirect_uri = RedirectUri.AbsoluteUri,
        response_type = "code",
        scope = "email"
    });

    return Redirect(loginUrl.AbsoluteUri);
}
public ActionResult FacebookCallback(string code)
{
    var fb = new FacebookClient();
    dynamic result = fb.Post("oauth/access_token", new
    {
        client_id = "xxxxxx",
        client_secret = "xxxx",
        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, like email, first name, middle name etc
    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;
    if (ModelState.IsValid)
    {
        User _user = new User();
        _user.FullName = firstname;
        _user.Email = email;
        _user.PhoneNo = "";
        // _user.Password = model.Security.Password;
        //_user.RetypePassword = model.Security.RetypePassword;
        //_user.CityId = model.Security.CityId;
        //_user.IsUpdate = model.Security.IsUpdate;
        _user.StatusId = (byte)Utilities.Status.Active;
        _user.EmailConfirmed = true;
        _user.Image = "/images/business.png";
        _user.CreatedDate = DateTime.Now;
        _user.ModifiedDate = DateTime.Now;
        _user.CreatedBy = 1;
        var userObj = seuritybll.Insert(_user);
        Session["User"] = me;
        Session["UserName"] = firstname;
        return RedirectToAction("Index", "Home");
        //return RedirectToAction("RegistrationSuccessfull");
    }
    // Set the auth cookie
    FormsAuthentication.SetAuthCookie(email, false);
    return RedirectToAction("Index", "Home");
}





我的尝试:



我在asp.net mvc网站上使用上面的代码登录facebook

但是使用这个代码我只能从我的facebook帐户登录。当我从其他帐户登录时它给出了错误就像你的应用程序不是在开发者选项中创建我跟着这个链接

你可以帮我吗

https:// www。 .com / Articles / 577384 /介绍到OAuth-in-ASP-NET-MVC

推荐答案

请参考 Docs / create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on.md at master·aspnet / Docs·GitHub [ ^ ]


这篇关于如何在ASP.NET MVC网站上登录面书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆