c# - What is the proper way to impement Form Authentication along side Facebook/Other authentication in ASP.NET MVC -
c# - What is the proper way to impement Form Authentication along side Facebook/Other authentication in ASP.NET MVC -
i building application in asp.net mvc 3 in have implemented forms authentication.
i give user alternative sign-up/log-in facebook account(and perchance other social accounts in future.)
i using c# facebook sdk
i have implemented facebook login workflow. question is, how handle mixing both forms , facebook auth? can't seem find sufficient examples of how accomplish this.
in regards facebook implementation, requesting permission user non-expiring auth token store in database.
for have this
create custom class (currentidentity) implements iidentity. override .tostring() class, , have sort of serialized state of object. had utilize "|" seperator. string stored in encrypted cookie.create session cookie
public static httpcookie authcookie(currentidentity identity) { //create authticket, store in cookie , redirect user formsauthenticationticket authticket = new formsauthenticationticket(1, identity.name, datetime.now, datetime.now.addhours(3), true, identity.tostring(), formsauthentication.formscookiepath); string encryptedticket = formsauthentication.encrypt(authticket); httpcookie authcookie = new httpcookie(formsauthentication.formscookiename, encryptedticket); authcookie.expires = authticket.expiration; homecoming authcookie; }
add cookie response.
httpresponse res = httpcontext.current.response; res.cookies.add(helper.authcookie(identity)); res.redirect(formsauthentication.getredirecturl(identity.name, true)); res.end();
now in gloabl.asax, within *application_authenticaterequest* read cookie , populate currentidentity object, this.
if (request.isauthenticated == true) { // extract forms authentication cookie string cookiename = formsauthentication.formscookiename; httpcookie authcookie = context.request.cookies[cookiename]; if (null == authcookie) { // there no authentication cookie. return; } formsauthenticationticket authticket = null; seek { authticket = formsauthentication.decrypt(authcookie.value); } grab (exception) { // log exception details (omitted simplicity) return; } if (null == authticket) { // cookie failed decrypt. return; } // when ticket created, userdata property assigned // pipe delimited string of role names. string[] userinfo = authticket.userdata.split('|'); // create identity object formsidentity id = new formsidentity(authticket); //populate currentidentity, serialized string currentidentity currentidentity = new currentidentity(userinfo[0], userinfo[1], userinfo[2]); system.security.principal.genericprincipal principal = new system.security.principal.genericprincipal(currentidentity, userinfo); context.user = principal; }
this should solve problem. have implemented similar thing on company's website.
c# asp.net asp.net-mvc facebook facebook-c#-sdk
Comments
Post a Comment