Facebook和asp.net会员的整合

问题描述:

我想允许用户使用Facebook注册我的网站。我已经在客户端使用JS SDK进行身份验证和授权,并且在服务器端使用C#sdk来获取完成用户配置文件所需的所有信息。 我的问题是,我为了让用户使用他的Facebook帐户登录,我需要在我的会员供应商中为他创建一个用户名和密码。 我已经决定使用Facebook电子邮件作为用户名,但是我可以怎样处理密码?我以后如何认证他?Facebook和asp.net会员的整合

谢谢!

编辑:什么我其实问的是:一旦用户通过Facebook认证的第一次,我有他所有的信息,我怎么会去马平这些信息给会员用户(用户名的条件和密码),我下一次他想用他已经创建的用户登录时,我怎么会将他重新登录(在响应中发送一个memebrship cookie)。我知道我每次都需要通过Facebook进行身份验证,但是我正在考虑如何处理asp.net会员方面,而不是Facebook方面的事情。对不起,如果我以前不清楚:)

Facebook使用OAuth 2.0。对用户进行身份验证的唯一方法是将用户发送到Facebook登录页面,然后使用他们的OAuth实现进行重定向。这本质上为您提供了一个用户特定的时间有限access_token

虽然可以请求offline_access permission,它可以让您代表用户随时执行授权请求,但它对于MembershipProviders并不是一个好的选择。用户必须同意这一点。 Facebook T & C是你不能否认用户访问您的网站,因为不同意扩展权限,所以这再次打破了模型。

我并不是说这不可能,但这似乎不值得付出努力。如果没有成员身份,只需使用表单身份验证部分就可以了。设置验证cookie &验证基于Facebook的验证cookie access_token

对于使用Facebook进行身份验证,请查看开源.NET OAuth库。我还写了一篇关于Facebook集成的文章,其中展示了一些集成技术(和观点)here。根据您修正问题

更新

使用窗体身份验证cookie来存储您选择(Facebook的用户名实例)的唯一标识符。您还可以将当前的access_token存储在Cookie中。

if (OAuth.ValidateUser(username, user_access_token)) 
{ 
    // store the user access token to make it available on each request 
    string userData = user_access_token; 

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
    1,          // ticket version 
    username,        // authenticated username 
    DateTime.Now,       // issueDate 
    DateTime.Now.AddMinutes(30),   // expiryDate 
    isPersistent,       // true to persist across browser sessions 
    userData,        // can be used to store additional user data 
    FormsAuthentication.FormsCookiePath); // the path for the cookie 

    // Encrypt the ticket using the machine key 
    string encryptedTicket = FormsAuthentication.Encrypt(ticket); 

    // Add the cookie to the request to save it 
    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)); 

    // Your redirect logic 
    Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent)); 
} 

然后重写验证请求以读取cookie并检索用户详细信息。

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
    HttpCookie authCookie = Request.Cookie[FormsAuthentication.FormsCookieName]; 
    if(authCookie != null) 
    { 
     //Extract the forms authentication cookie 
     FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); 

     // If caching roles in userData field then extract 
     string user_access_token = authTicket.UserData; 

     // Create the IIdentity instance, maybe use a custom one 
     // possibly retrieve extra data from database or whatever here 
     IIdentity id = new FacebookIdentity(authTicket, user_access_token); 

     // Create the IPrinciple instance 
     IPrincipal principal = new GenericPrincipal(id, new string[]{}); 

     // Set the context user 
     Context.User = principal; 
    } 
} 
在每个请求的代码

然后:

var username = Context.User.Identity.Username; 
// implement as an extension method on IIdentity which types to 
// FacebookIdentity to get user_access_token 
var user_access_token = Context.User.Identity.Access_Token; 
+0

对不起,如果我不清楚这不是我有问题的部分我编辑了这篇文章详细说明 –

+0

感谢您的信息我查了这些函数不会j使用FormsAuthentication.SetAuthCookie是否足以将用户从服务器端登录?我已经有了一个提供商,我只用一键注册就可以使用Facebook。我不需要Facebook上的其他任何东西。 –

+0

是的,但您只能为标识符存储一个字段。你真的想保留'access_token'和'username'我想。当然,你可以只为它们划界,但它不够干净。 – TheCodeKing

1选项:你应该生成一个密码,并通过电子邮件发送给用户,例如:

欢迎来到我的superwebsite .com,登录:foo @ foo。com密码:dhyfd46

然后如果您看到密码是原始(生成),请求用户更改为另一个。

第二种选择:你永远不会使用密码,因为你的帐户是一个元组(facebookID,电子邮件)(你会从facebookApi获得它),如果它匹配这个(用户将点击om'登录与Facebook”,你就可以创建用户会话

编辑后:

为什么不使用元组(用户ID,facebookId),如果你有FB ID您知道用户id

+0

对不起,如果我不清楚。我编辑了这篇文章。 –

+0

猜我可以做到这一点,但我已经知道这是用户,只要我有一个令牌,所以没有一种方法可以在服务器端登录用户,并在没有需要的情况下在响应中返回cookie密码? –

+0

只是想通过电子邮件以明文形式发送用户密码非常不安全。我不会在任何情况下推荐它。 –