2015-05-08 24 views
1

我想實現谷歌外部登錄使用owin,我不需要保存用戶在Db中,我發現所有演示都綁定到EntityFramework,所以我試圖剝離下來,並得到了一個工作實現那就是:裸露的最小歐文OAuth外部登錄

public partial class Startup 
{ 
    public void ConfigureAuth(IAppBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login") 
     }); 

     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     app.UseGoogleAuthentication("myid00000000000.apps.googleusercontent.com", "mysecret"); 
    } 
} 

和控制器:

public class AccountController : Controller 
{ 
    private const string XsrfKey = "XsrfId"; 

    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult ExternalLogin(string provider, string returnUrl) 
    { 
     // Request a redirect to the external login provider 
     return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); 
    } 

    // 
    // GET: /Account/ExternalLoginCallback 
    [AllowAnonymous] 
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
    { 
     var loginInfo = await HttpContext.GetOwinContext().Authentication.GetExternalLoginInfoAsync(); 
     if (loginInfo == null) 
     { 
      return Content("failed"); 
     } 

     return Content("logged in"); 
    } 

internal class ChallengeResult : HttpUnauthorizedResult 
... 

現在我不知道是否這就夠了,如果我沒有錯過一些用戶驗證步驟, 當我打電話Authentication.GetExternalLoginInfoAsync(),我得到一個非空的結果是否意味着我可以確定loginInfo中的信息實際上是來自Google,還是我需要向Google做一些額外的請求以檢查此信息?

+0

不,你應該沒問題,你有什麼。但是,如果您不信任它,請調試並檢查發生的所有事情,以便您自行驗證它是否正常工作。 –

+0

@ChrisPratt當我調試它loginInfo有個人資料信息名稱,電子郵件,來自谷歌,這只是我第一次試驗這個,我認爲我需要驗證這個信息,猜測owin已經做到了 –

回答

0

假設你存儲Authentication.GetExternalLoginInfoAsync的輸出()中的變量「LOGININFO」

現在,檢查loginInfo.Result.Login.LoginProvider的價值。如果值爲「Google」,那麼您收到的信息來自Google。