public class OpenIdController : Controller { private static OpenIdRelyingParty openIdProvider = new OpenIdRelyingParty(); public ActionResult Index(string userOpenId) { // . IAuthenticationResponse response = openIdProvider.GetResponse(); // response null, OpenID . if (response == null) { Identifier id; // OpenID . if (Identifier.TryParse(userOpenId, out id)) { try { // OpenID IAuthenticationRequest request = openIdProvider.CreateRequest(userOpenId); return request.RedirectingResponse.AsActionResult(); } catch (ProtocolException ex) { TempData["error"] = ex.Message; } } return RedirectToAction("Index", "Login"); } else { // OpenID switch (response.Status) { // case AuthenticationStatus.Authenticated: { TempData["id"] = response.ClaimedIdentifier; return RedirectToAction("Index", "Main"); } case AuthenticationStatus.Canceled: { TempData["message"] = " "; return RedirectToAction("Index", "Main"); } case AuthenticationStatus.Failed: { TempData["message"] = " ."; TempData["error"] = response.Exception.Message; return RedirectToAction("Index", "Main"); } default: { return RedirectToAction("Index", "Main"); } } } }}
try { // OpenID IAuthenticationRequest request = openIdProvider.CreateRequest(userOpenId); FetchRequest fetch = new FetchRequest(); fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email, true)); fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.First, true)); fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.Last, true)); fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Preferences.Language, true)); request.AddExtension(fetch); return request.RedirectingResponse.AsActionResult(); }
case AuthenticationStatus.Authenticated: { var fetches = response.GetExtension<FetchResponse>(); if (fetches != null) { string str = string.Empty; str += string.Format("Email : {0} <br/>", fetches.Attributes[WellKnownAttributes.Contact.Email].Values[0]); str += string.Format(" : {0} <br/>", fetches.Attributes[WellKnownAttributes.Name.First].Values[0]); str += string.Format(" : {0} <br/>", fetches.Attributes[WellKnownAttributes.Name.Last].Values[0]); str += string.Format(" : {0} <br/>", fetches.Attributes[WellKnownAttributes.Preferences.Language].Values[0]); TempData["info"] = str; } TempData["id"] = response.ClaimedIdentifier; return RedirectToAction("Index", "Main"); }
try { // OpenID IAuthenticationRequest request = openIdProvider.CreateRequest(userOpenId); ClaimsRequest claim = new ClaimsRequest(); claim.BirthDate = DemandLevel.Require; claim.Country = DemandLevel.Require; claim.Email = DemandLevel.Require; claim.FullName = DemandLevel.Require; claim.Gender = DemandLevel.Require; claim.Language = DemandLevel.Require; claim.Nickname = DemandLevel.Require; claim.PostalCode = DemandLevel.Require; claim.TimeZone = DemandLevel.Require; request.AddExtension(claim); return request.RedirectingResponse.AsActionResult(); }
// case AuthenticationStatus.Authenticated: { var claims = response.GetExtension<ClaimsResponse>(); if (claims != null) { string str = string.Empty; str += string.Format(" : {0} <br/>", claims.BirthDate); str += string.Format(": {0}<br/>", claims.Country); str += string.Format("Email: {0}<br/>", claims.Email); str += string.Format(" : {0}<br/>", claims.FullName); str += string.Format(": {0}<br/>", claims.Gender); str += string.Format(": {0}<br/>", claims.Language); str += string.Format(": {0}<br/>", claims.Nickname); str += string.Format(": {0}<br/>", claims.PostalCode); str += string.Format(" : {0}<br/>", claims.TimeZone); TempData["info"] = str; } TempData["id"] = response.ClaimedIdentifier; return RedirectToAction("Index", "Main"); }
case AuthenticationStatus.Failed: { // DoTNetOpenAuth var email = Request.Params["openid.sreg.email"]; var fullname = Request.Params["openid.sreg.fullname"]; var nickname = Request.Params["openid.sreg.nickname"]; if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(fullname) || string.IsNullOrEmpty(nickname)) { TempData["message"] = " ."; TempData["error"] = response.Exception.Message; } else { AuthOpenID("http://id.rambler.ru/users/" + nickname, fullname); } return RedirectToAction("Index", "Main"); }
<configSections> <section name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection" requirePermission="false" allowLocation="true"/> </configSections> ... <dotNetOpenAuth> <messaging clockSkew="60:00:00" lifetime="10:00:00" strict="true"> <untrustedWebRequest timeout="00:10:00" readWriteTimeout="00:01:00" maximumBytesToRead="1048576" maximumRedirections="10"> ... </untrustedWebRequest> </messaging> </dotNetOpenAuth>
// case AuthenticationStatus.Authenticated: { string str = string.Empty; str += string.Format("Email: {0}<br/>", Request.Params["openid.sreg.email"]); str += string.Format(" : {0}<br/>", Request.Params["openid.sreg.fullname"]); str += string.Format(": {0}<br/>", Request.Params["openid.sreg.gender"]); str += string.Format(": {0}<br/>", Request.Params["openid.sreg.nickname"]); TempData["info"] = str; TempData["id"] = response.ClaimedIdentifier; return RedirectToAction("Index", "Main"); } </code>
graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope=email
graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}
access_token=114782445239544|2.izExIa_6jpjjhUy3s7EDZw__.3600.1291809600-708770020|VbEE8KVlLTMygGmYwm-V08aVKgY&expired=12010
graph.facebook.com/me?access_token={0}
VK.init({ apiId: vkontakteAppId });
VK.Widgets.Auth("vk_auth", { width: "210px", authUrl: '/vkontakte' });
public class VkontakteController : Controller { public ActionResult Index(string first_name, string last_name, string uid) { var str = string.Empty; str += string.Format(" : {0}<br/>", first_name); str += string.Format(": {0}<br/>", last_name); TempData["info"] = str; TempData["id"] = "http://vkontakte.ru/id" + uid; return RedirectToAction("Index", "Main"); } }
Source: https://habr.com/ru/post/109700/
All Articles