我有一个使用sql成员资格提供程序构建在ASP.NET 3.5和SQL Server 2005中的网站,并且可能是表单身份验证.
由于我网站上的安全需求非常低,我想进行一次身份验证,然后无限期地保持登录(不给用户选择).
发生的事情是用户登录,保持登录会话,然后下次他们到达时,他们会被注销.
如何保持登录?
这是技术细节,我已经尝试了许多持续时间的变化.
Try
If Membership.ValidateUser(UserName.Text, Password.Text) Then
Security.UserManager.AuthenticateUser(UserName.Text)
If FormsAuthentication.GetRedirectUrl(UserName.Text, False) = "/default.aspx" Then
Try
'Custom Logic'
Catch Ex As Exception
'Custom Error handling'
End Try
Else
FormsAuthentication.RedirectFromLoginPage(UserName.Text, True)
End If
End If
Catch ex As Exception
RaiseEvent ExceptionThrown(New ApplicationException("An error occurred trying to log the user in after account creation.", ex))
End Try
Public Shared Sub AuthenticateUser(ByVal Username As String)
Dim Expiration As DateTime = DateTime.Now.AddMonths(3)
Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(Username, …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-membership asp.net-3.5 sqlmembershipprovider
我有一个MVC 4网站,用户可以登录,我保存一个cookie及其会话信息,这样他们就不必再次登录了.
public void SetCookie(HttpCookie cookie)
{
HttpContext.Current.Response.Cookies.Set(cookie);
}
Run Code Online (Sandbox Code Playgroud)
这适用于桌面设备,但是当我在iOS上运行它时,它在100%的时间内都不起作用.如果我在移动浏览器(Chrome或Safari)中打开至少1页并导航回我的网站,则会找到我的会话cookie,而不必登录.但是,如果我关闭该浏览器的所有窗口,那么下次我导航回我的网站时,找不到会话cookie.我在我的cookie上设置了1年的持续时间/到期时间,因此它没有到期.
到目前为止,我唯一发现的是这是.NET 4.0中的一个错误,并在.NET 4.5中得到修复.我相信我已经在运行.NET 4.5,通过查看我的*.csproj和TargetFrameworkVersion元素:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{xxxxxxxxxxxxxxxxxx}</ProjectGuid>
<ProjectTypeGuids>{xxxxxxxx};{xxxxxxxxxxxxx};{xxxxxxxxxxxxxxxx}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MyApp</RootNamespace>
<AssemblyName>MyApp</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
这是.NET(或MVC)的错误,还是我的编码?我是否需要升级到MVC 5(而不是升级到.NET 4.5的原始建议)?
这真的被窃听我,因为在未来的未来几个月大部分的流量到我的网站将是移动用户,我可能会失去他们中的一些,如果他们要保持记录的每次(我知道我讨厌不得不在移动设备上登录...)
编辑:
顺便说一句 - 这是关于同一主题的另一个页面:使用iPhone UIWebView时的Asp.Net Forms身份验证
我也试过实现这个,它也没有用:
http://www.bloggersworld.com/index.php/asp-net-forms-authentication-iphone-cookies/
以上内容包括Scott Hanselmans建议修复:
http://www.hanselman.com/blog/FormsAuthenticationOnASPNETSitesWithTheGoogleChromeBrowserOnIOS.aspx
这是我的cookie创建代码,以防它有用:
private void CreateAndSetAuthenticationCookie(int loginId, string username)
{ …Run Code Online (Sandbox Code Playgroud)