Asp.net mvc 在OpenID脱机提供程序中成功(假)登录后重定向用户
很多天前,我问过,我已经准备好了一个离线OpenID提供者(我猜)Asp.net mvc 在OpenID脱机提供程序中成功(假)登录后重定向用户,asp.net-mvc,authentication,openid,offline,Asp.net Mvc,Authentication,Openid,Offline,很多天前,我问过,我已经准备好了一个离线OpenID提供者(我猜) 我想要的是OpenID标识符的简单登录文本框,它将自动接受用户并考虑用户登录,然后我希望他重定向到主产品页面(产品=>索引)。 但是,我不知道(在互联网上也没有发现)在假身份验证过程之后如何继续 我试着这样做: [HttpPost] public ActionResult Login(string openid)//openid is the identifier taken from the login textbox {
我想要的是OpenID标识符的简单登录文本框,它将自动接受用户并考虑用户登录,然后我希望他重定向到主产品页面(产品=>索引)。 但是,我不知道(在互联网上也没有发现)在假身份验证过程之后如何继续
我试着这样做:[HttpPost]
public ActionResult Login(string openid)//openid is the identifier taken from the login textbox
{
var rely = new OpenIdRelyingParty();
var req = rely.CreateRequest(openid);
req.RedirectToProvider();
return RedirectToAction("Index", "Products");
}
首先,它没有在第4行被重定向(而是刷新登录页面),其次,没有经过身份验证的用户,我的意思是当我在Watch window中选中user
时,它不是空的,但用户名是空的,并且没有经过身份验证的身份,我想,这意味着没有设置Cookie
p.S:
1.没有抛出异常。但当我尝试调用FormsAuthentication.RedirectFromLogin()方法时,出现了一个异常(设置HTTP头后,服务器无法修改Cookie)。
2.索引操作方法用[Authorize]属性标记,因此当有人试图浏览产品时,它会被重定向到登录屏幕,但当他登录(假登录)时,他不应该被重定向回产品页面吗
我也试过:
[HttpPost]
public ActionResult Login(string openid)
{
var rely = new OpenIdRelyingParty();
return rely.CreateRequest(openid).RedirectingResponse.AsActionResult();
}
但是没有运气
我错过了什么?如何使其按预期工作?我可以依靠我自己,但我需要一份关于OpenID的体面文档,特别是对于离线本地提供商。检查此示例:
public ActionResult OpenId(字符串openIdUrl)
{
var response=Openid.GetResponse();
如果(响应==null)
{
//用户提交标识符
标识符id;
if(Identifier.TryParse(openIdUrl,out id))
{
尝试
{
var request=Openid.CreateRequest(openIdUrl);
var fetch=new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
请求。添加扩展(获取);
return request.RedirectingResponse.AsActionResult();
}
捕获(协议例外)
{
_logger.Error(“OpenID异常…”,ex);
返回重定向操作(“登录操作”);
}
}
_logger.Info(“OpenID错误…无效url.url=”+openIdUrl+”);
返回重定向操作(“登录”);
}
//OpenID提供程序发送断言响应
开关(响应状态)
{
案例验证状态。已验证:
var fetch=response.GetExtension();
string firstName=“未知”;
字符串lastName=“未知”;
字符串email=“未知”;
if(fetch!=null)
{
firstName=fetch.GetAttributeValue(WellKnownAttributes.Name.First);
lastName=fetch.GetAttributeValue(WellKnownAttributes.Name.Last);
email=fetch.GetAttributeValue(WellKnownAttributes.Contact.email);
}
//认证
FormsAuthentication.SetAuthCookie(用户名:email,createPersistentCookie:false);
//重定向
返回操作(“索引”、“产品”);
案例身份验证状态。已取消:
_Info(“OpenID:在提供者处取消”);
返回重定向操作(“登录”);
案例身份验证状态。失败:
_logger.Error(“OpenID异常…”,response.Exception);
返回重定向操作(“登录”);
}
返回重定向操作(“登录”);
}
基本上,动作方法会被调用两次:
第一次由用户提交表单
第二次是来自OpenID提供程序的回调(重定向)。
这一次,响应将有一个值。如果response.Status
有效,您可以使用该类登录您的用户,最后您可以将其重定向到您的主产品页面。检查此示例:
public ActionResult OpenId(字符串openIdUrl)
{
var response=Openid.GetResponse();
如果(响应==null)
{
//用户提交标识符
标识符id;
if(Identifier.TryParse(openIdUrl,out id))
{
尝试
{
var request=Openid.CreateRequest(openIdUrl);
var fetch=new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
请求。添加扩展(获取);
return request.RedirectingResponse.AsActionResult();
}
捕获(协议例外)
{
_logger.Error(“OpenID异常…”,ex);
返回重定向操作(“登录操作”);
}
}
_logger.Info(“OpenID错误…无效url.url=”+openIdUrl+”);
返回重定向操作(“登录”);
}
//OpenID提供程序发送断言响应
开关(响应状态)
{
案例验证状态。已验证:
var fetch=response.GetExtension();
string firstName=“未知”;
字符串lastName=“未知”;
字符串email=“未知”;
if(fetch!=null)
{
firstName=fetch.GetAttributeValue(WellKnownAttributes.Name.First);
lastName=fetch.GetAttributeValue(WellKnownAttributes.Name.Last);
email=fetch.GetAttributeValue(WellKnownAttributes.Contact.email);
}
//认证
public ActionResult OpenId(string openIdUrl)
{
var response = Openid.GetResponse();
if (response == null)
{
// User submitting Identifier
Identifier id;
if (Identifier.TryParse(openIdUrl, out id))
{
try
{
var request = Openid.CreateRequest(openIdUrl);
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
request.AddExtension(fetch);
return request.RedirectingResponse.AsActionResult();
}
catch (ProtocolException ex)
{
_logger.Error("OpenID Exception...", ex);
return RedirectToAction("LoginAction");
}
}
_logger.Info("OpenID Error...invalid url. url='" + openIdUrl + "'");
return RedirectToAction("Login");
}
// OpenID Provider sending assertion response
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
var fetch = response.GetExtension<FetchResponse>();
string firstName = "unknown";
string lastName = "unknown";
string email = "unknown";
if(fetch!=null)
{
firstName = fetch.GetAttributeValue(WellKnownAttributes.Name.First);
lastName = fetch.GetAttributeValue(WellKnownAttributes.Name.Last);
email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
}
// Authentication
FormsAuthentication.SetAuthCookie(userName: email, createPersistentCookie: false);
// Redirection
return RedirectToAction("Index", "Products");
case AuthenticationStatus.Canceled:
_logger.Info("OpenID: Cancelled at provider.");
return RedirectToAction("Login");
case AuthenticationStatus.Failed:
_logger.Error("OpenID Exception...", response.Exception);
return RedirectToAction("Login");
}
return RedirectToAction("Login");
}