2017-11-23 9 views
0

OWIN 라이브러리를 통해 Google 계정으로 사용자를 인증하는 ASP.NET MVC 애플리케이션이 있습니다.Gmail에 로그인하지 않고 OWIN으로 ASP.NET MVC 앱을 인증

사용자는 호스트 컴퓨터 (고객) 컴퓨터의 공용 웹 컴퓨터에 로그인하여 웹 응용 프로그램의 일부 데이터를 관리 할 수 ​​있습니다.

Google 로그인 페이지로 리디렉션되는 로그인 버튼이 있습니다. 사용자가 사용자 이름과 비밀번호를 입력하고이 웹 앱으로 다시 리디렉션됩니다. 지금까지는 괜찮습니다. 이것이 OWIN이 일반적으로 작동하는 방식입니다.

사용자가 앱을 로그 아웃하는 것을 잊어 버리고 누군가가 웹 앱에서 데이터를 캡처 할 염려가 있습니다. 이것은 모든 웹 응용 프로그램의 위험을 감수합니다. 동의해야합니다. 그러나 사용자가 로그 아웃하는 것을 잊어 버리면 누군가가 Gmail, Google 문서 및 모든 관련 Google 서비스의 모든 이메일을 캡처한다는 추가적인 위험이 있습니다.

Owin이 사용자를 Google 로그인 페이지로 리디렉션 한 후 Google은 Gmail 및 웹 브라우저의 모든 관련 Google 서비스에 로그인 사용자가 아닌 웹 앱에 권한을 부여하는 방식으로 Owin 인증을 수정하고자합니다.

다음 코드는 웹 앱 인증의 부작용으로 Gmail에도 로그인합니다. OWIN의 구성 옵션이 많이 있다는 것을 알았습니다. 달성 할 수 있다고 생각합니다.

public partial class Startup 
{ 
    private double _expirationTimeCookies = 30; // minutes - sliding expiration 

    public static ApplicationUserManager ApplicationUserManagerCreate(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     var userStore = new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()); 
     var tokenProvider = new DataProtectorTokenProvider<ApplicationUser>(options.DataProtectionProvider.Create("ASP.NET Identity")); 

     ApplicationUserManager manager = new ApplicationUserManager(userStore); 

     manager.EmailService = new EmailService(); 
     manager.SmsService = new SmsService(); 

     var dataProtectionProvider = options.DataProtectionProvider; 
     if (dataProtectionProvider != null) 
     { 
      manager.UserTokenProvider = tokenProvider; 
     } 

     return ApplicationUserManager.Create(tokenProvider, manager); 
    } 

    public void ConfigureAuth(IAppBuilder app) 
    {   
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManagerCreate); 
     app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      ExpireTimeSpan = TimeSpan.FromMinutes(_expirationTimeCookies), 

      LoginPath = new PathString("/Account/Login"), 
      Provider = new CookieAuthenticationProvider 
      { 
       // Enables the application to validate the security stamp when the user logs in. 
       // This is a security feature which is used when you change a password or add an external login to your account. 
       OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
        validateInterval: TimeSpan.FromMinutes(_expirationTimeCookies), 
        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager, Helpers.IdentityExtensions.OnlineSettingsForAccount(user.Id))) 
      } 
     });      
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() 
     { 
      ClientId = "xxxxxxxxxx.apps.googleusercontent.com", 
      ClientSecret = "xxxxxxx", 
      Provider = new GoogleOAuth2AuthenticationProvider() 
      {      
       OnApplyRedirect = delegate(GoogleOAuth2ApplyRedirectContext context) 
       {      
        string redirect = context.RedirectUri; 
        redirect += "&prompt=select_account"; 
        context.Response.Redirect(redirect); 
       } 
      }, 
     }); 
    } 
} 
+1

따라서 앱이 공용 컴퓨터에서 실행되며, 사용자가 로그 아웃하는 것을 잊어 버리면 다음 사용자가 이전 사용자의 Gmail 및 기타 Google 서비스를 열 수 없도록하고 싶습니다. – CodeCaster

+0

그리고 Google에서 사용자를 Google 서비스에 로그인하지 않고 어떻게 확인할 것입니까? – Evk

+0

@CodeCaster 예, 정확하게. – qub1n

답변

7

Google은 이러한 옵션을 제공하지 않습니다. openid \ oauth 토큰을 얻기 위해 Google로 리디렉션하면 사용자에게 사용자의 신원을 확인하기 위해 Google에 먼저 로그인해야합니다 (아직 로그인하지 않은 경우). 그러면 사용자의 신원을 확인한 후 신청서에 토큰을 발급합니다. 이론 상으로는 Google이 사용자 이름과 비밀번호를 확인하고 Google과의 세션을 설정하지 않고 토큰을 발급 할 수있는 옵션을 제공 할 수 있지만 그 이유는 없습니다. 가능한 한 로그인 한 사용자를 유지하는 것이 Google에 유익하며 또한 이러한 인증 흐름의 주요 목표, 즉 가능한 희귀 한 비밀번호를 입력하는 것을 방해합니다.

그래서 토큰을 얻은 후에도 사용자는 Google 서비스에 로그인되어 있으며 변경할 수 없습니다. 당신이 할 수있는 일은 토큰을 얻은 직후 구글의 대상자를 로그 아웃하는 것입니다. 이것은 구글 서비스 오프 사용자를 기록합니다

https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://yoursite.com; 

후 다시 사이트로 그를 리디렉션 :이를 위해 다음과 같은 URL로 사용자를 리디렉션 할 수 있습니다. 지금 작동하지만 그것은 미래에 약간의 시간이 작동을 멈출 수 있도록

  1. , 그것은, (적어도 내가 그 잘 모르는 것 같아요) 어디 문서화되지 않습니다 것을

    참고.

  2. 그런 일을하는 것은 매우 일 수 있습니다. 사이트에 로그인 할 때마다 사용자가 Google에서 로그 오프됩니다. 나는 개인적으로 그러한 일을하는 사이트를 결코 사용하지 않을 것이다. (사소한 일이 아니기 때문에 어떤 일이 일어나는지 알아낼 수있을 것이다.)

그래서 나는이 방법을 사용하는 대신 (이 클릭 않다면 로그인 양식의 "공용 컴퓨터"체크 박스를 소개하고 정보를 보여줌으로써) 공용 컴퓨터에 이러한 인증의 위험에 대한 사용자 지시하지 제안, 또는 것 적어도 - 사용자가 명시 적으로 자신이 공용 컴퓨터에 있다고 분명하게 알리는 경우에만이 작업을 수행하지 마십시오.