2011-02-26 11 views
11

मुझे पता है कि इसके बारे में अन्य प्रश्न हैं, लेकिन वे पुराने हैं, और मुझे वह जवाब नहीं मिल रहा है जो कहीं भी विश्वसनीय होगा।स्टैक ओवरफ़्लो द्वारा वास्तव में ओपनआईडी समाधान का उपयोग किया जाता है?

स्टैक   द्वारा वास्तव में उपयोग किए जाने वाले उपयोगकर्ताओं के लिए ओवरफ्लो का उपयोग वास्तव में किया जाता है? साइट DotNetOpenAuth का दावा है कि यह है। लेकिन मेरे लिए सबसे अधिक (दृश्यमान) समान दिखता है OAuth C# Library

तो यह वास्तव में क्या उपयोग करता है? या मैं एक ही यूआई की नकल कैसे कर सकता हूं?

मैं एएसपी.नेट एमवीसी का उपयोग कर एक ही ओपनआईडी प्रमाणीकरण बनाना चाहता हूं।

+0

मुझे लगता है कि इस प्रश्न को विशेष प्रकार की टिप्पणी की आवश्यकता है। चूंकि यह पूछा गया था कि यह पूछा गया था कि यह स्टैक ओवरफ्लो मेटा में स्थानांतरित हो गया है, फिर मुख्य स्टैक ओवरफ्लो पर, मेटा को फिर से और मुख्य पर वापस ले जाया गया है। ऐसा लगता है कि यह दोनों साइटों या उनमें से गैर से संबंधित है? मेरा मानना ​​है कि यह स्टैक ओवरफ्लो मुख्य से संबंधित है, जहां मैंने इसे मूल रूप से पोस्ट किया था। ऐसा इसलिए है क्योंकि मैंने एसओ साइट के बारे में जिज्ञासा के कारण नहीं पूछा था, क्योंकि मैं उसी समाधान, उसी तकनीक का उपयोग करना चाहता था। इसलिए लिरिक का जवाब मेरे प्रश्न का बहुत बड़ा जवाब है, लेकिन इस तरह के उत्तर में मेटा (बहुत तकनीकी) पर कुछ भी नहीं है और इसलिए यह यहां रहता है। – drasto

उत्तर

17

स्टैक ओवरव्लो DotNetOpenAuth का उपयोग करता है।

blog से

:

लेकिन सौभाग्य से हम एंड्रयू Arnott, खुला स्रोत DotNetOpenAuth पुस्तकालय उपयोग हम

15

मैं साथ OpenID प्रमाणीकरण प्राप्त करने में सक्षम था के प्राथमिक लेखक के साथ एक उत्कृष्ट संवाद है DotNetOpenAuth मेरी वेबसाइट पर (www.mydevarmy.com) काफी कम समय में (ध्यान दें कि मैं एएसपी.नेट, एमवीसी, डॉटनेट ओपेनएथ, आदि के लिए कुल noob हूं)।

DotNetOpenAuth विभिन्न नमूनों के साथ आता है और वे भी एक ASP.NET MVC नमूना है, लेकिन वे केवल यह है कि नमूने में एक दृश्य और नियंत्रक प्रदान करते हैं और वे वास्तव में एक मॉडल जो MVC में एम है की जरूरत नहीं है :) । इसके बाद मैं इतने पर निम्नलिखित प्रश्न पूछा:

What are the responsibilities of the components in an MVC pattern for a simple login

तो कैसे होगा एक बहुत सरल OpenID लॉगिन MVC में की तरह लग रहे? ठीक है, एक नजर डालें ...

1. आप एक मॉडल की आवश्यकता होगी:

public class User 
{ 
    [DisplayName("User ID")] 
    public int UserID{ get; set; } 

    [Required] 
    [DisplayName("OpenID")] 
    public string OpenID { get; set; } 
} 

public class FormsAuthenticationService : IFormsAuthenticationService 
{ 
    public void SignIn(string openID, bool createPersistentCookie) 
    { 
     if (String.IsNullOrEmpty(openID)) throw new ArgumentException("OpenID cannot be null or empty.", "OpenID"); 

     FormsAuthentication.SetAuthCookie(openID, createPersistentCookie); 
    } 

    public void SignOut() 
    { 
     FormsAuthentication.SignOut(); 
    } 
} 

2. आप एक नियंत्रक की आवश्यकता होगी:

[HandleError] 
public class UserController : Controller 
{ 
    private static OpenIdRelyingParty openid = new OpenIdRelyingParty(); 
    public IFormsAuthenticationService FormsService { get; set; } 

    protected override void Initialize(RequestContext requestContext) 
    { 
     if (FormsService == null) 
     { 
      FormsService = new FormsAuthenticationService(); 
     } 

     base.Initialize(requestContext); 
    } 

    // ************************************** 
    // URL: /User/LogIn 
    // ************************************** 
    public ActionResult LogIn() 
    { 
     if (User.Identity.IsAuthenticated) 
     { 
      return RedirectToAction("Profile", "User"); 
     } 

     Identifier openID; 
     if (Identifier.TryParse(Request.QueryString["dnoa.userSuppliedIdentifier"], out openID)) 
     { 
      return LogIn(new User { OpenID = openID }, Request.QueryString["ReturnUrl"]); 
     } 
     else 
     { 
      return View(); 
     } 
    } 

    [HttpPost] 
    public ActionResult LogIn(User model, string returnUrl) 
    { 
     string openID = ModelState.IsValid?model.OpenID:Request.Form["openid_identifier"]; 

     if (User.Identity.IsAuthenticated) 
     { 
      return RedirectToAction("Profile", "User"); 
     } 
     else if (!string.IsNullOrEmpty(openID)) 
     { 
      return Authenticate(openID, returnUrl); 
     } 
     else if(ModelState.IsValid) 
     { 
      ModelState.AddModelError("error", "The OpenID field is required."); 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

    // ************************************** 
    // URL: /User/LogOut 
    // ************************************** 
    public ActionResult LogOut() 
    { 
     if (User.Identity.IsAuthenticated) 
     { 
      FormsService.SignOut(); 
     } 

     return RedirectToAction("Index", "Home"); 
    } 

    // ************************************** 
    // URL: /User/Profile 
    // ************************************** 
    [Authorize] 
    public ActionResult Profile(User model) 
    { 
     if (User.Identity.IsAuthenticated) 
     { 
      // ------- YOU CAN SKIP THIS SECTION ---------------- 
      model = /*some code to get the user from the repository*/; 

      // If the user wasn't located in the database 
      // then add the user to our database of users 
      if (model == null) 
      { 
       model = RegisterNewUser(User.Identity.Name); 
      } 
      // -------------------------------------------------- 

      return View(model); 
     } 
     else 
     { 
      return RedirectToAction("LogIn"); 
     } 
    } 

    private User RegisterNewUser(string openID) 
    { 
     User user = new User{OpenID = openID}; 

     // Create a new user model 

     // Submit the user to the database repository 

     // Update the user model in order to get the UserID, 
     // which is automatically generated from the DB. 
     // (you can use LINQ-to-SQL to map your model to the DB) 

     return user; 
    } 

    [ValidateInput(false)] 
    private ActionResult Authenticate(string openID, string returnUrl) 
    { 
     var response = openid.GetResponse(); 
     if (response == null) 
     { 
      // Stage 2: user submitting Identifier 
      Identifier id; 
      if (Identifier.TryParse(openID, out id)) 
      { 
       try 
       { 
        return openid.CreateRequest(openID).RedirectingResponse.AsActionResult(); 
       } 
       catch (ProtocolException ex) 
       { 
        ModelState.AddModelError("error", "Invalid OpenID."); 

        ModelState.AddModelError("error", ex.Message); 
        return View("LogIn"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("error", "Invalid OpenID."); 
       return View("LogIn"); 
      } 
     } 
     else 
     { 
      // Stage 3: OpenID Provider sending assertion response 
      switch (response.Status) 
      { 
       case AuthenticationStatus.Authenticated: 
        Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay; 
        FormsAuthentication.SetAuthCookie(response.FriendlyIdentifierForDisplay, true); 
        if (!string.IsNullOrEmpty(returnUrl)) 
        { 
         return Redirect(returnUrl); 
        } 
        else 
        { 
         return RedirectToAction("Profile", "User"); 
        } 
       case AuthenticationStatus.Canceled: 
        ModelState.AddModelError("error", "Authentication canceled at provider."); 
        return View("LogIn"); 
       case AuthenticationStatus.Failed: 
        ModelState.AddModelError("error", "Authentication failed: " + response.Exception.Message); 
        return View("LogIn"); 
      } 
     } 
     return new EmptyResult(); 
    } 
} 

3। आपको एक दृश्य की आवश्यकता होगी:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YourProject.Models.User>" %> 

<asp:Content ID="loginTitle" ContentPlaceHolderID="TitleContent" runat="server"> 
    Log in - YourWebSiteName 
</asp:Content> 
<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server"> 
     <p> 
      <%--- If you have a domain, then you should sign up for an affiliate id with MyOpenID or something like that ---%> 
      Please log in with your OpenID or <a href="https://www.myopenid.com/signup?affiliate_id=????">create an 
       OpenID with myOpenID</a> if you don't have one. 
     </p> 
     <% 
     string returnURL = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); 
     if (returnURL == null) 
     { 
      returnURL = string.Empty; 
     } 

     using (Html.BeginForm("LogIn", "User", returnURL)) 
     {%> 
      <%= Html.LabelFor(m => m.OpenID)%>: 
      <%= Html.TextBoxFor(m => m.OpenID)%> 
      <input type="submit" value="Log in" /> 
     <% 
     } %> 

     <%--- Display Errors ---%> 
     <%= Html.ValidationSummary()%> 
</asp:Content> 

ध्यान दें कि मैंने आपको Profile व्यू प्रदान नहीं किया है, लेकिन यह पता लगाने के लिए पर्याप्त सरल होना चाहिए।

+0

इसके लिए धन्यवाद! यह बहुत अच्छा जवाब है!मैं इसे आज़माउंगा और यदि यह काम करता है तो आपको इसे अपने ब्लॉग पर लिखना चाहिए यदि आपके पास कुछ है। उम्मीद है कि यह और अधिक वोट मिला है। मुझे ओडेड के एंडवर को स्वीकार करना है क्योंकि मैंने पहले ही इसे प्रोमिस कर दिया है। – drasto

+0

@drasto, कोई समस्या नहीं ... मुझे मेटा पर अंक प्राप्त करने में दिलचस्पी नहीं है और ओडेड के पास और भी अधिक वोट हैं। मुझे उम्मीद है कि यह सहायक होगा :) – Kiril

+1

@drasto, मैंने इसके बारे में एक ब्लॉग पोस्ट बनाया: http://codesprout.blogspot.com/2011/03/using-dotnetopenauth-to-create-simple.html – Kiril

संबंधित मुद्दे