mozilla

Compare Revisions

Quick Setup

Change Revisions

Revision 311951:

Revision 311951 by Callahad on

Revision 314411:

Revision 314411 by sergiotapia on

Title:
Quick Setup
Quick Setup
Slug:
Persona/Quick_Setup
Persona/Quick_Setup
Content:

Revision 311951
Revision 314411
tt207      For an example on how to use Persona in a C# ASP.Net MVC3 a
 >pplication, <a href="https://github.com/sergiotapia/ASP.Net-MVC3-
 >Persona-Demo" title="https://github.com/sergiotapia/ASP.Net-MVC3-
 >Persona-Demo">visit this application demo</a>&nbsp;or see the Con
 >troller code below:
208    </p>
209    <p>
210      &nbsp;
211    </p>
212    <pre>
213public class AuthController : Controller
214{
215    [HttpPost]
216    public ActionResult Login(string assertion)
217    {
218        if (assertion == null)
219        {
220            // The 'assertion' key of the API wasn't POSTED. Redi
 >rect, 
221            // or whatever you'd like, to try again.
222            return RedirectToAction("Index", "Home");
223        }
224 
225        using (var web = new WebClient())
226        {
227            // Build the data we're going to POST.
228            var data = new NameValueCollection();
229            data["assertion"] = assertion;
230            data["audience"] = "https://example.com:443"; // Use 
 >your website's URL here.
231 
232 
233            // POST the data to the Persona provider (in this cas
 >e Mozilla)
234            var response = web.UploadValues("https://verifier.log
 >in.persona.org/verify", "POST", data);
235            var buffer = Encoding.Convert(Encoding.GetEncoding("i
 >so-8859-1"), Encoding.UTF8, response);
236 
237 
238            // Convert the response to JSON.
239            var tempString = Encoding.UTF8.GetString(buffer, 0, r
 >esponse.Length);
240            var reader = new JsonReader();
241            dynamic output = reader.Read(tempString);
242 
243            if (output.status == "okay")
244            {
245                string email = output.email; // Since this is dyn
 >amic, convert it to string.
246                FormsAuthentication.SetAuthCookie(email, true);
247                return RedirectToAction("Index", "Home");    
248            }
249           
250            // Could not log in, do something else.
251            return RedirectToAction("Index", "Home");
252        }
253    }
254 
255    [HttpPost]
256    public ActionResult Logout()
257    {
258        // Just sign the user out of the Forms Authentication fra
 >mework.
259        FormsAuthentication.SignOut();
260        return RedirectToAction("Index", "Home");
261    }
262}
263</pre>
264    <p>

Back to History