2010-11-29 51 views
2

我一直在寻找重构我的登录控制器以获得更好的代码可读性。在此过程中,我遇到看起来像这样DotNetOpenAuth - “视图”如何与此交互

using DotNetOpenAuth.Messaging; 

public ActionResult LogOn() 
{ 
    var openid = new OpenIdRelyingParty(); 
    IAuthenticationResponse response = openid.GetResponse(); 

    if (response != null) 
    { 
     switch (response.Status) 
     { 
      case AuthenticationStatus.Authenticated: 
       FormsAuthentication.RedirectFromLoginPage(
        response.ClaimedIdentifier, false); 
       break; 
      case AuthenticationStatus.Canceled: 
       ModelState.AddModelError("loginIdentifier", 
        "Login was cancelled at the provider"); 
       break; 
      case AuthenticationStatus.Failed: 
       ModelState.AddModelError("loginIdentifier", 
        "Login failed using the provided OpenID identifier"); 
       break; 
     } 
    }   

    return View(); 
} 

[System.Web.Mvc.AcceptVerbs(HttpVerbs.Post)] 
public ActionResult LogOn(string loginIdentifier) 
{ 
    if (!Identifier.IsValid(loginIdentifier)) 
    { 
     ModelState.AddModelError("loginIdentifier", 
        "The specified login identifier is invalid"); 
     return View(); 
    } 
    else 
    { 
     var openid = new OpenIdRelyingParty(); 
     IAuthenticationRequest request = openid.CreateRequest(
      Identifier.Parse(loginIdentifier)); 

     // Require some additional data 
     request.AddExtension(new ClaimsRequest 
     { 
      BirthDate = DemandLevel.NoRequest, 
      Email = DemandLevel.Require, 
      FullName = DemandLevel.Require 
     }); 

     return request.RedirectingResponse.AsActionResult(); 
    } 
} 

Programmatic OpenID Relying Party example来到现在这看起来堆比我从下载的示例中使用更清洁和更容易阅读。 (我下载最新的版本,这是他们给出的例子 - 这是我建我的应用程序在4个月前同样的例子)

[ValidateInput(false)] 
    public ActionResult Authenticate(string returnUrl) { 
     var response = openid.GetResponse(); 
     if (response == null) { 
      // Stage 2: user submitting Identifier 
      Identifier id; 
      if (Identifier.TryParse(Request.Form["openid_identifier"], out id)) { 
       try { 
        return openid.CreateRequest(Request.Form["openid_identifier"]).RedirectingResponse.AsActionResult(); 
       } catch (ProtocolException ex) { 
        ViewData["Message"] = ex.Message; 
        return View("Login"); 
       } 
      } else { 
       ViewData["Message"] = "Invalid identifier"; 
       return View("Login"); 
      } 
     } else { 
      // Stage 3: OpenID Provider sending assertion response 
      switch (response.Status) { 
       case AuthenticationStatus.Authenticated: 
        Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay; 
        FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false); 
        if (!string.IsNullOrEmpty(returnUrl)) { 
         return Redirect(returnUrl); 
        } else { 
         return RedirectToAction("Index", "Home"); 
        } 
       case AuthenticationStatus.Canceled: 
        ViewData["Message"] = "Canceled at provider"; 
        return View("Login"); 
       case AuthenticationStatus.Failed: 
        ViewData["Message"] = response.Exception.Message; 
        return View("Login"); 
      } 
     } 
     return new EmptyResult(); 
    } 

现在例子有,如果我喜欢的语句太多,和我必须添加额外的处理(activity loggingchecking for new useradd to existing account),它开始变得非常混乱。

不幸的是,如果我想重构我的代码,看起来更像第一个例子,我就陷入了一个小问题。视图如何与此交互?我的意思是,它正在寻找openid.GetResponse(),但我如何提交该回复?

就像我说的,如果我能得到这个工作,它看起来好像比我现在的方式要干净得多。

+0

你是如何得到最后一道工作的? `return request.RedirectingResponse.AsActionResult();` 我已经添加了一些命名空间usings,但是我无法弄清楚如何改进这部分。 HttpRequestBase不包含RedirectingResponse的引用。 – zeristor 2013-06-28 10:53:44

回答

3

您不提交此回复。当您点击批准或取消批准页面时,OpenID提供程序会执行此操作。据我了解,这里发生的事情是,例如谷歌通过GET返回一堆数据,然后DotNetOpenAuth解析当您拨打openid.GetResponse()

我已经发布了一个备受注目的基本OpenID for MVC实现,它还具有用户注册功能。你可以在http://mvcopenid.codeplex.com找到它。它还不如上面的样品干净,但我觉得它很干净。我会最终重构,但我需要弄清楚如何绕过MVC的模型绑定很好,因为我不喜欢像Request.Form["openid_identifier"]这样的代码。

+1

嘿,看起来很酷。我要检查出来! – 2010-11-29 15:53:09

相关问题