gpt4 book ai didi

asp.net-mvc - DotNetOpenAuth - "View"如何与此交互

转载 作者:行者123 更新时间:2023-12-04 06:40:31 25 4
gpt4 key购买 nike

我一直在考虑重构我的登录 Controller 以获得更好的代码可读性。在这样做的过程中,我遇到了 Programmatic OpenID Relying Party example看起来像这样

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();
}
}

现在,这看起来比我从可下载示例中使用的更干净、更易于阅读。 (我下载了最新版本,这是他们给出的示例 - 这与我在 5 个月前构建我的应用程序的示例相同。)
    [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();
}

现在该示例有太多我喜欢的 if 语句,并且我必须添加额外的处理( activity loggingchecking for new useradd to existing account ),它开始变得非常困惑非常快。

不幸的是,如果我要重构我的代码使其看起来更像第一个示例,我就会遇到一个小问题。 View 如何与此交互?我的意思是,它正在寻找 openid.GetResponse() ,但我该如何提交该回复?

就像我说的那样,如果我能做到这一点,看起来它会比我目前的方式干净得多。

最佳答案

您不提交此回复。当您在其批准页面上单击批准或取消时,OpenID 提供商会执行此操作。据我了解,这里发生的情况是,例如 Google 通过 GET 返回一堆数据,然后当您调用 openid.GetResponse() 时,DotNetOpenAuth 会解析。 .

我已经为 MVC 实现发布了一个受到大量评论的基本 OpenID,它还具有用户注册功能。您可以在 http://mvcopenid.codeplex.com 上找到它.它仍然没有上面的 sample 那么干净,但我觉得它很干净。我最终会重构它,但我需要弄清楚如何很好地绕过 MVC 的模型绑定(bind)器,因为我不喜欢像 Request.Form["openid_identifier"] 这样的代码.

关于asp.net-mvc - DotNetOpenAuth - "View"如何与此交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4301214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com