有Attributes和DotNetOpenID的问题

时间:2009-05-19 12:44:23

标签: asp.net-mvc dotnetopenauth

我正在尝试设置我的代码以从OpenID服务提供商请求一些属性。与此同时,我希望返回一个ActionResult。

以下是OpenId示例MVC项目中的代码(直接来自DotNetOpenId / DotNetOpenAuth网站)......

try
{
    return openid.CreateRequest(Request.Form["openid_identifier"])
        .RedirectingResponse
        .AsActionResult();
 }
 catch (ProtocolException ex)
 { ... }

这很好,但它没有显示我请求任何可选或必需的属性。 所以,我尝试了以下(我自己的,未经测试的pseduo-ish代码)....

var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Alias);
fetch.Attributes.AddRequired(WellKnownAttributes.Preferences.Language);
fetch.Attributes.AddRequired(WellKnownAttributes.Preferences.TimeZone);
fetch.Attributes.AddRequired(WellKnownAttributes.Person.Gender);

openId.CreateRequest(identifier.ToString()).AddExtension(fetch);

这似乎也没问题。但是......我现在不知道如何要求结果成为ActionResult()......

如何创建一个openId.CreateRequest,允许我定义一些必需的属性并返回一个ActionView?

1 个答案:

答案 0 :(得分:1)

你做得很好。只需将这两种方法结合起来。

致电IAuthenticationRequest.AddExtension后,只需致电IAuthenticationRequest.RedirectingResponse.AsActionResult()

try
{
    var request = openid.CreateRequest(Request.Form["openid_identifier"]);

    var fetch = new FetchRequest();
    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
    fetch.Attributes.AddRequired(WellKnownAttributes.Name.Alias);
    fetch.Attributes.AddRequired(WellKnownAttributes.Preferences.Language);
    fetch.Attributes.AddRequired(WellKnownAttributes.Preferences.TimeZone);
    fetch.Attributes.AddRequired(WellKnownAttributes.Person.Gender);
    request.AddExtension(fetch);

    return request.RedirectingResponse.AsActionResult();
}
catch (ProtocolException ex)
{ ... }