我有一个与远程WCF Web服务对话的网站.两者都使用相同的自定义FormsAuthentication Provider.我想通过模拟当前登录站点的用户的WCF服务进行身份验证.我已经使用UserName客户端凭据手动执行此操作,但我需要知道用户密码.那么,最有效的是:经过身份验证的用户发出请求,我创建了一个服务客户端并设置了他的凭据:
serviceClient.ClientCredentials.UserName.UserName = username;
serviceClient.ClientCredentials.UserName.Password = password;
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是直接传递FormsAuthentication cookie,因为我不想存储用户密码.
有任何想法吗?
在ASP.NET中,FormsAuthenticationModule拦截任何HTTP 401,并返回HTTP 302重定向到登录页面.这对AJAX来说很痛苦,因为你要求json并在html中获取登录页面,但状态代码是HTTP 200.
在ASP.NET Web API中避免这种拦截的方法是什么?
在ASP.NET MVC4中,通过明确结束连接来防止此拦截非常容易:
public class MyMvcAuthFilter:AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest() && !filterContext.IsChildAction)
{
filterContext.Result = new HttpStatusCodeResult(401);
filterContext.HttpContext.Response.StatusCode = 401;
filterContext.HttpContext.Response.SuppressContent = true;
filterContext.HttpContext.Response.End();
}
else
base.HandleUnauthorizedRequest(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在ASP.NET Web API中我无法显式结束连接,因此即使我使用此代码,FormsAuthenticationModule也会拦截响应并将重定向发送到登录页面:
public class MyWebApiAuth: AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if(actionContext.Request.Headers.Any(h=>h.Key.Equals("X-Requested-With",StringComparison.OrdinalIgnoreCase)))
{
var xhr = actionContext.Request.Headers.Single(h => h.Key.Equals("X-Requested-With", StringComparison.OrdinalIgnoreCase)).Value.First();
if (xhr.Equals("XMLHttpRequest", StringComparison.OrdinalIgnoreCase))
{
// this does not work either
//throw new HttpResponseException(HttpStatusCode.Unauthorized);
actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); …Run Code Online (Sandbox Code Playgroud) asp.net forms-authentication asp.net-authorization asp.net-mvc-4 asp.net-web-api
我正在使用mvc .net应用程序,我正在使用表单身份验证.我希望在用户进行身份验证后将用户重定向到他请求的页面.任何帮助,将不胜感激.
authentication asp.net-mvc forms-authentication asp.net-mvc-3
我已将SQL-Server Reporting Services 2012(SSRS 2012)切换为表单身份验证,以便我们可以通过Internet使用它.
我无法在任何地方找到SSRS 2012的表单身份验证示例,因此我必须使用SSRS 2008R2,并将其调整为2012年的单点登录(SSO).
在那一刻,一切似乎按预期工作; 我甚至设法让SSO跨域工作.
但现在我有一个问题:
我正在使用谷歌浏览器测试所有报告(超过200个),因为我必须插入一个改变td border-size的JavaScript,因为HTML显示在非IE5-QuirksMode中.在大约第50次报告之后,我突然得到:
"HTTP 400错误请求 - 请求太长"
在那之后,我无法查看任何其他报告,甚至是那些之前没有工作的报告.
问题似乎是由于太多的cookie造成的,事实上,当我删除一些"*_SKA"(Session Keep Alive?)cookie时,它又开始工作了.

我现在的问题是我不知道导致这种"cookie溢出"的原因.我也不知道,如果这是Chrome中的错误,香草SSRS中的错误或新表单身份验证导致的错误.
我在新表单中所做的一切 - 与cookie有关的身份验证是这样的:
using System;
using System.Collections.Generic;
using System.Text;
namespace FormsAuthentication_RS2012
{
internal class FormsAuthenticationWorkaround
{
public static void RedirectFromLoginPage(string strUser, bool createPersistentCookie)
{
//string url = System.Web.Security.FormsAuthentication.GetRedirectUrl(strUser, true);
string url = GetRedirectUrlWithoutFailingOnColon(strUser, createPersistentCookie);
SQL.Log("User: '" + strUser + "' ReturnUrl", url);
if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Response != null)
System.Web.HttpContext.Current.Response.Redirect(url);
}
// https://github.com/mono/mono/blob/master/mcs/class/System.Web/System.Web.Security/FormsAuthentication.cs
// @MSFT: WTF are …Run Code Online (Sandbox Code Playgroud) asp.net cookies forms-authentication reporting-services ssrs-2012
我有一个webforms网站,正在调用我们正在开发的新MVC6网站.用户将使用表单身份验证在webforms网站上一直登录,然后重定向到新的MVC6网站.我知道在MVC6中我应该使用Cookie身份验证,但无法让它解密cookie.我怀疑它是关于web.config和machinekey的变化,但我真的被卡住了.
这就是我所做的.
我已按如下方式设置了cookie身份验证
app.UseCookieAuthentication(options =>
{
options.CookieName = "MyWebformsCookie";
options.AutomaticAuthenticate = true;
options.AuthenticationScheme = "Cookies";
options.TicketDataFormat = new MySecureDataFormat();
options.DataProtectionProvider = new MyDataProtectionProvider();
//options.CookieDomain = "localhost";
});
Run Code Online (Sandbox Code Playgroud)
课程如下
public class MySecureDataFormat : ISecureDataFormat<AuthenticationTicket>
{
public string Protect(AuthenticationTicket data)
{
return string.Empty;
}
public string Protect(AuthenticationTicket data, string purpose)
{
return string.Empty;
}
public AuthenticationTicket Unprotect(string protectedText)
{
return null;
}
public AuthenticationTicket Unprotect(string protectedText, string purpose)
{
var ticket = FormsAuthentication.Decrypt(protectedText);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
正在读取cookie,并调用Unprotect方法,但随后在FormsAuthentication.Decrypt方法上出现错误并出现错误
System.Web.dll中出现"System.Web.HttpException"类型的异常,但未在用户代码中处理
其他信息:无法验证数据.
Stack = …
我刚刚注意到使用ASP.NET应用程序中的表单身份验证访问CSS文件时出现问题.
在我登录之前,我没有使用我在登录页面中设置的任何样式,因为IIS似乎阻止登录页面访问此文件.
有一个简单的解决方案吗?
如果会话已过期且用户单击指向另一个webform的链接,则asp.net身份验证会自动将用户重定向到登录页面.
但是,有些用户没有点击指向其他网络表单的链接.例如:在gridviews中编辑链接,当使用带有文本框的AutoCompleteExtender并且应用程序尝试获取信息时,基本上,在每种情况下,当回发完成并且事件不是由asp.net身份验证自动处理时.
处理这些异常的最佳方法是什么?
更新:我刚刚修改了问题标题:表单身份验证超时,而不是初始会话超时.谢谢你让我意识到这种差异.
更新:我刚刚针对我面临的具体问题创建了一个新问题:如何使用UpdatePanel处理因过期身份验证票据而导致的异常?.令人惊讶的是,我没有找到关于它的更多信息.我将衷心感谢您的帮助.
我想在我的网站上手动实现基于表单的身份验证.
我正在使用Web.config文件进行数据存储
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="~/Admin/OrderHistory.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false"
>
<credentials passwordFormat="Clear">
<user name="Admin" password="adm123$"/>
<user name="Administrator" password="adm234%"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users ="?" />
<allow users = "*" />
</authorization>
Run Code Online (Sandbox Code Playgroud)
Login.aspx在根目录中有一个页面使用ASP.NET登录控件来获取用户名和密码.
一切正常,但当用户logged in手动转到login.aspx页面时,它不会将用户重定向到defaultUrl页面.
我想将用户重定向到特定页面/ defaultUrl页面,如果他已登录并手动登录到login.aspx页面
怎么做?
if (FormsAuthentication.Authenticate(LoginUser.UserName, LoginUser.Password))
{
FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Facebook C#SDK,我通过他们的Facebook帐户验证用户身份.一旦我执行了所有检查以"验证"它们,我就打电话FormsAuthentication.SetAuthCookie(email, false);
执行该调用是否允许我访问User.Identity.IsAuthenticated我的操作?如果我没有打电话怎么办?
我有一些操作将根据其身份验证状态返回不同的视图,并希望确保User.Identity.IsAuthenticated在MVC 4应用程序中可靠.
我想要ASP.NET中的表单身份验证的好处.我希望它能继续授权我这样,但是我的情况有一点不同; 我想对一个简单的Web服务(特别是由客户端提供)进行身份验证.
我有我的代码来查看Web位置并查看它们是否应该被授权,但是如何在ASP.NET中设置cookie [?]或授权标志,他们知道当前用户已获得授权.
基本上...
if (HttpContext.Current.User.Identity.IsAuthenticated)
// we're all good
//Other wise...
bool success = CheckClientsWebService(string username, string password);
if (success)
// Somehow tell .NET that they're authorized
Run Code Online (Sandbox Code Playgroud)
*注意:这是一个相当简单的服务,不处理组或角色.只需检查用户是否可以查看该网站.
asp.net ×7
c# ×3
asp.net-mvc ×2
cookies ×2
.net ×1
encryption ×1
exception ×1
ssrs-2012 ×1
wcf ×1
wcf-security ×1