Tom*_*bes 0 c# asp.net-mvc google-authentication owin
我得到了ASP.NET MVC应用程序,它通过OWIN库通过Google帐户验证用户.
用户有时会在主机(客户)计算机,公共计算机上登录Web应用程序来管理Web应用程序中的某些数据.
有一个登录按钮,可将您重定向到Google登录页面,用户输入用户名和密码,然后重定向回此Web应用程序.到目前为止,这是好的,这就是OWIN通常的工作方式.
我担心用户忘记注销应用程序而有人会从Web应用程序捕获数据.这在任何网络应用程序中都存在风险,我必须接受它.但是,如果用户忘记退出,则会有额外的风险,即有人会捕获来自Gmail,Google文档和所有相关Google服务的所有电子邮件.
我希望修改Owin身份验证,以便在Owin将用户重定向到Google登录页面后,Google只会将授权传递给Web应用,而无需登录用户访问Gmail以及Web浏览器中的所有相关Google服务.
以下代码也会登录Gmail作为Web应用程序身份验证的副作用.我看到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);
}
},
});
}
}
Run Code Online (Sandbox Code Playgroud)
Google不提供此类选项.当您重定向到谷歌获取openid\oauth令牌时,它将始终要求用户首先登录谷歌(如果尚未登录)以验证用户的身份,并且只有在此之后才会为您的应用程序发出令牌.从理论上讲,谷歌可以提供一个选项来验证用户名和密码并发出令牌而不与谷歌本身建立会话,但我认为他们没有理由这样做.保持用户尽可能多地登录对谷歌有利,这也会干扰这种认证流程的主要目标,即 - 尽可能少地输入密码.
因此,在您获得令牌后,用户也会登录到Google服务,但您无法更改.但你可以做的是,获得令牌后立即注销谷歌的目标人.为此,您可以将用户重定向到以下网址:
https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://yoursite.com;
Run Code Online (Sandbox Code Playgroud)
这将使用户关闭谷歌服务,然后将他重定向回您的网站.
注意:
虽然它现在可以使用,但它没有记录在任何地方(至少我不知道),所以它可能会在将来停止工作.
这样做对用户来说可能非常烦人.每次登录您的网站后,用户将从谷歌注销.我个人永远不会使用网站做这样的事情(在我能够弄清楚发生了什么之后,因为它并非无足轻重).
所以我建议不要使用这种方法,而是指导用户在公共计算机上进行此类身份验证的危险(通过在登录表单上引入"公共计算机"复选框并显示已点击的信息),或者至少 - 不要这样做总是这样,但前提是用户明确告诉你他现在在公共计算机上.
| 归档时间: |
|
| 查看次数: |
626 次 |
| 最近记录: |