我正在构建一个ASP.NET网站,它使用FormsAuthentication和标准的Session机制配置如下:
<authentication mode="Forms">
<forms cookieless="UseCookies" name=".MyAppAuth" loginUrl="~\Login.aspx" timeout="20"/>
</authentication>
...
<sessionState timeout="20" cookieless="UseCookies" />
Run Code Online (Sandbox Code Playgroud)
似乎身份验证cookie的生命周期不等于用户Session的生命周期.所以ASP.NET并不能保证这一点
用户注销时会话终止,
在用户注销之前,会话不会终止.
有没有办法定制FormsAuthentication或\和会话状态机制来实现这些目标?
这是应该非常简单的事情之一,我无法弄清楚为什么它不起作用.
我正在尝试为ASP.net 3.5应用程序设置一些非常快速的身份验证,但是将用户名和密码存储在web.config文件中(我知道它不是很安全,但它是一个内部应用程序,我不断被要求添加和删除登录,这是最快的方法).
所以,相关的配置部分如下所示:
<authentication mode="Forms">
<forms loginUrl="~/login.aspx">
<credentials>
<user name="user" password="password" />
<user name="user2" password="password2" />
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)
并且,在登录页面中,代码如下所示:
string username = tbUsername.Text;
string password = tbPassword.Text;
if (FormsAuthentication.Authenticate(username, password))
FormsAuthentication.RedirectFromLoginPage(username, false);
Run Code Online (Sandbox Code Playgroud)
但是,FormsAuthentication.Authenticate(用户名,密码)始终返回false.我无法弄清楚原因.
我甚至尝试使用Membership.ValidateUser但只是在本地数据库中添加到App_Data文件夹.
有什么东西真的很基本我忘了这里或者这在.net 3.5中根本不起作用吗?
asp.net authentication authorization forms-authentication web-config
到目前为止,我一直在使用MVC区域作为我的mvc应用程序的管理部分,但是最近我一直在重新思考这个问题,因为每个应用程序的表单身份验证不能有多个配置.
这已经成为一个问题,因为在最近的一个项目中我想设置auth cookie不会让用户过期,但我不想这对管理用户来说.我也不希望用户登录页面用于访问管理工具.
我正在考虑在解决方案中专门为管理工具设置一个单独的MVC项目.这在我看来是最好的选择,但我想知道部署的复杂性. (目前我在VS2008中使用Web部署项目来管理构建).
有没有人目前在管理部分使用单独的MVC项目?任何陷阱?关于为什么这不是一个好主意的其他意见?
谢谢.
我正在使用以下代码创建自己的身份验证票证:
string formsCookieStr = string.Empty;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // version
username, // user name
DateTime.Now, // issue time
DateTime.Now.AddMinutes(30), // expires
false, // Persistence
userRoleData // user data
);
formsCookieStr = FormsAuthentication.Encrypt(ticket);
HttpCookie FormsCookie = new HttpCookie(FormsAuthentication.FormsCookieName, formsCookieStr);
HttpContext.Response.Cookies.Add(FormsCookie);
Run Code Online (Sandbox Code Playgroud)
我希望到期时间是一个滑动到期 - 每次客户端发送请求时,到期时间应重置为30分钟.但是,我只是在用户第一次登录时创建票证.ASP.NET是否会自动为我延长到期时间,或者我是否需要"手动"执行某些操作来实现滑动到期?
这些代码行之间有什么区别:
<forms timeout="5" />
<membership userIsOnlineTimeWindow="5" />
<sessionState timeout="5" />
Run Code Online (Sandbox Code Playgroud)
非常感谢.
asp.net forms-authentication asp.net-membership web-config session-state
我需要在当前登录用户的FormsAuthentication AuthCookie中编辑userdata.我不知道如何找出当前用户是否选择了持久性cookie("记住我").
//user is already logged in...
HttpCookie authCookie = FormsAuthentication.GetAuthCookie(username, ispersistant); //how to I determine 'ispersistant'?
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, NEWuserdata);
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
HttpContext.Current.Response.SetCookie(authCookie);
Run Code Online (Sandbox Code Playgroud)
有人有任何想法吗?谢谢
我在MVC5站点中使用以下代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel loginModel) {
if (ModelState.IsValid) {
var authenticated = FormsAuthentication.Authenticate(loginModel.UserName, loginModel.Password);
if (authenticated) {
FormsAuthentication.SetAuthCookie(loginModel.UserName, true);
return RedirectToAction("AdminPanel");
}
ModelState.AddModelError("", "The username and password combination were incorrect");
}
return View(loginModel);
}
Run Code Online (Sandbox Code Playgroud)
这引发了以下警告:
System.Web.Security.FormsAuthentication.Authenticate(string,string)'已过时:'建议的替代方法是使用Membership API,例如Membership.ValidateUser.有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=252463.
首先是免责声明 - 我是那些喜欢与事物保持同步的开发者之一,我更愿意完全避免VS中的警告.但是,在这个特定的实例中,我使用非常原始的身份验证,它直接来自web.config,如下所示:
<authentication mode="Forms">
<forms loginUrl="~/account/login" timeout="2880" slidingExpiration="true" name=".ASPXFORMSAUTH">
<credentials passwordFormat="SHA1">
<user name="MyUserName" password="a-big-long-password-hash"/>
</credentials>
</forms>
</authentication>
Run Code Online (Sandbox Code Playgroud)
在这个项目中绝对没有进一步的登录要求 - 使用数据库是过度的,并且不需要分布式登录; 基本上,在web.config中存储细节是最理想的解决方案,每个应用程序可能只有一个用户.我无疑会从控制器(使用DI/IoC)中抽象出认证代码,但我仍然打算使用FormsAuthentication对象对web.config中的详细信息进行身份验证.
虽然我完全了解会员提供商,DotNetOpenAuth和新的(但非常可怕的)基于OWIN的身份验证模型,但上述代码绰绰有余.
第一个问题 - 为什么当这是一个完全有效的用例时,FormsAuthentication已经过时了?我知道FormsAuthentication是一个密封的黑盒子,难以测试,其背后的代码是特定于域的,但在这个特定的实例中(我想直接从web.config部分读取详细信息),似乎很疯狂编写会员提供者或自定义身份验证服务?
第二个问题 - 除非我遗漏了某些内容,否则新的OWIN模型用于分布式身份验证,不适用于基于角色的企业级安全系统.从我读过的很多博客文章,白皮书和SO帖子来看,它似乎仍然不完整.我的假设是对的吗?OWIN是所有安全系统的未来,还是应该继续编写更适合我客户需求的定制安全系统?
我目前正在为汽车经销商开展一个大项目,我陷入两难境地.
我应该使用ASP.NET身份还是旧式的FormsAuthentication?
我需要能够通过2个提供商登录.首先,用户总是在数据库中,但是我们检查它是否是LDAP用户,如果是,则通过LDAP进行身份验证(我使用WebService作为具有Login方法的用户).
这是我的登录方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginModel model)
{
if (ModelState.IsValid)
{
var userInDb = this.db.Users.FirstOrDefault(u => u.Username == model.username);
if (userInDb != null)
{
// USER EXISTS
if (userInDb.IsLdap)
{
try
{
// IS LDAP POWERED, IGNORE PASSWORD IN DB
using (var ws = WebServiceClient.Factory(model.GetDomain()))
{
// MAKE AUTH
var result = await ws.Login(model.GetUsername(), model.password);
if (result.Success)
{
// USER IS LEGAL
FormsAuthentication.SetAuthCookie(model.username, model.remember);
return RedirectToAction("Init");
}
else
{
// USER IS ILLEGAL
ModelState.AddModelError("", "Username …Run Code Online (Sandbox Code Playgroud) 我已经打了好几个小时了,我很难过.我正在向MVC 5控制器发出ajax post请求,试图自动登录特定的预定义"超级"用户.在控制器方法中,我正在尝试以编程方式设置HttpContext.Current.User并进行身份验证,因此超级用户可以跳过手动登录的过程.对此的共识似乎就在这里,我实现了:
这似乎有效,直到我尝试使用自定义AuthorizeAttribute查看任何其他控制器方法.
控制器方法:
[HttpPost]
[AllowAnonymous]
public ActionResult Login(string username)
{
string password = ConfigurationManager.AppSettings["Pass"];
User user = service.Login(username, password);
var name = FormsAuthentication.FormsCookieName;
var cookie = Response.Cookies[name];
if (cookie != null)
{
var ticket = FormsAuthentication.Decrypt(cookie.Value);
if (ticket != null && !ticket.Expired)
{
string[] roles = (ticket.UserData as string ?? "").Split(',');
System.Web.HttpContext.Current.User = new GenericPrincipal(new FormsIdentity(ticket), roles);
}
}
//...processing result
return Json(result);
}
Run Code Online (Sandbox Code Playgroud)
上面的service.Login方法创建cookie:
FormsAuthentication.SetAuthCookie(cookieValue, false);
Run Code Online (Sandbox Code Playgroud)
虽然我正在设置具有Identity和IsAuthenticated的User,但下面的filterContext.HttpContext.User不是同一个用户.它本质上是空的,好像从未分配过,并且未经过身份验证.
public override void OnAuthorization(AuthorizationContext filterContext)
{
string[] userDetails = …Run Code Online (Sandbox Code Playgroud) 我有两个报告,一个是内部用户,另一个是外部用户,
对于内部用户,我需要启用表单身份验证才能查看报告,而不是在服务器中创建用户帐户.
对于外部用户,我不想启用任何身份验证,以便他们可以从浏览器访问报告而无需任何身份验证.
我按照以下步骤使用来自以下链接的SSRS样本,在完成所有更改后我得到HTTP500错误.请帮助启用此基于表单的身份验证.
修改RSReportServer.config文件
步骤1:-
<Authentication>
<AuthenticationTypes>
<Custom/>
</AuthenticationTypes>
<EnableAuthPersistence>true</EnableAuthPersistence>
</Authentication>
Run Code Online (Sandbox Code Playgroud)
第2步:-
<Security>
<Extension Name="Forms"
Type="Microsoft.Samples.ReportingServices.CustomSecurity.Authorization,
Microsoft.Samples.ReportingServices.CustomSecurity" >
<Configuration>
<AdminConfiguration>
<UserName>username</UserName>
</AdminConfiguration>
</Configuration>
</Extension>
</Security>
<Authentication>
<Extension Name="Forms" Type="Microsoft.Samples.ReportingServices.CustomSecurity.AuthenticationExtension, Microsoft.Samples.ReportingServices.CustomSecurity" />
</Authentication>
Run Code Online (Sandbox Code Playgroud)
第3步: -
<UI>
<CustomAuthenticationUI>
<loginUrl>/Pages/UILogon.aspx</loginUrl>
<UseSSL>True</UseSSL>
</CustomAuthenticationUI>
<ReportServerUrl>http://<server>/ReportServer</ReportServerUrl>
</UI>
Run Code Online (Sandbox Code Playgroud)
修改RSSrvPolicy.config文件
第4步:-
<CodeGroup
class="UnionCodeGroup"
version="1"
Name="SecurityExtensionCodeGroup"
Description="Code group for the sample security extension"
PermissionSetName="FullTrust">
<IMembershipCondition
class="UrlMembershipCondition"
version="1"
Url="C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\bin\Microsoft.Samples.ReportingServices.CustomSecurity.dll"
/>
</CodeGroup>
Run Code Online (Sandbox Code Playgroud)
修改RSMgrPolicy.config文件
第五步: -
<CodeGroup
class="FirstMatchCodeGroup"
version="1" …Run Code Online (Sandbox Code Playgroud) sql-server forms-authentication reporting-services ssrs-2008 msbi
asp.net ×6
c# ×3
asp.net-mvc ×2
web-config ×2
.net ×1
cookies ×1
httpcontext ×1
msbi ×1
msbuild ×1
owin ×1
persistence ×1
security ×1
session ×1
sql-server ×1
ssrs-2008 ×1