标签: formsauthentication

FormsAuthentication.SignOut()使用javascript

有谁请帮助我解决这个问题,在我的asp.net应用程序中,我使用FormsAuthentication.SignOut(); 注销应用程序的方法,但我有一个奇怪的要求,我必须使用Javascript函数实现FormsAuthentication.SignOut().可能吗?如果是,请通过提供示例代码来帮助我实现此要求.

提前致谢

javascript c# asp.net formsauthentication

3
推荐指数
1
解决办法
2994
查看次数

如何在AWS EC2中同步机器密钥?

由于Auto-Scaling不允许缓冲时间和/或不够智能,不允许只有"新请求"进入由于自动缩放而被安排关闭的实例,我试图避免使用粘性 - ELB提供的会议.否则,此行为将使一些客户具有登录屏幕.

该网站和其他RESTful Web服务正在IIS7中运行.该网站使用基于经典表单的身份验证(MVC3中的内置成员资格提供程序),但很快将迁移到自定义成员资格提供程序以查看cookie并解密然后给出判决.RESTful Web服务已经在使用自定义成员资格提供程序.

在这种情况下,我认为对于我不能通过其ELB使用粘性会话而无法获得的网站.

RESTful Web服务的ELB可以配置为非粘性,因为它们会在每个请求上单独观察cookie,以查看auth令牌是否存在某些加密值.但是,这里的问题是FormsAuthentication.Enrypt和FormsAuthentication.Decrypt方法,它们使用MachineKeys.对 ?!如果没有,那么它没有问题,但如果他们这样做,那么如何在EC2中的自动标量实例之间同步机器密钥?

amazon-ec2 machinekey formsauthentication autoscaling

3
推荐指数
1
解决办法
967
查看次数

使用formsauthentication进行登录并使用HttpContext.Current.User.Identity

我创建了一个网页,其中包含一个包含剃刀形式的页面.用户可以登录此表单,然后重定向到其他页面.登录(和注销)成功地与formsauthentication一起使用.但是,我似乎无法使用HttpContext.Current.User.Identity.Name来检索存储的用户名(在formsauthentication cookie中).它返回一个空字符串"".

我使用的是MVC 5和ASP 4.5,没有标准的成员资格或角色提供者.

登录:

 [HttpPost]
        public ActionResult Login(User user)
        {
            if (ModelState.IsValid)
            {
                bool authenticated = userscontroller.isAuthorized(user.Email, user.Password);
                if (authenticated)
                {
                    if (userscontroller.isAuthenticated())
                    {
                        userscontroller.deAuthenticateUser();
                    }
                    userscontroller.authenticateUser(user);
                    return Redirect(Url.Action("Index", "Home"));
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

验证用户:

 public void authenticateUser(User user)
    {
        FormsAuthentication.SetAuthCookie(user.Username, false);
    }
Run Code Online (Sandbox Code Playgroud)

然后获取用户的名称:

public User userFromCookie()
{
    if (isAuthenticated())
    {
        return getUserByUsername(HttpContext.Current.User.Identity.Name);
    }
    else { return null; }
}
Run Code Online (Sandbox Code Playgroud)

isauthenticated()

public bool isAuthenticated()
{
    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
    {
        return true;
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

Webconfig:

<authentication …
Run Code Online (Sandbox Code Playgroud)

c# model-view-controller formsauthentication

3
推荐指数
1
解决办法
1万
查看次数

在.NET 3.5中回发后,FormsAuthentication不保留UserData字段

FormsAuthenticationTicket从零开始创建了一个,但发现在以后检索它时,UserData它不会回来.这是使用的代码:

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1,
                        user.UserId,
                        DateTime.Now,
                        DateTime.MaxValue,
                        false,
                        user.UserType);

HttpCookie cookie = new HttpCookie(
     FormsAuthentication.FormsCookieName, 
     FormsAuthentication.Encrypt(ticket));

Response.Cookies.Add(cookie);
Run Code Online (Sandbox Code Playgroud)

但是,当读到下一个时Request,我发现该UserData字段现在是空的:

string encryptedCookie = Request.Cookies[ FormsAuthentication.FormsCookieName ].Value;
FormsAuthenticationticket ticket = FormsAuthentication.Decrypt(encryptedCookie);
Assert.IsTrue( ticket.UserData.Length == 0 ); //TRUE!
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

asp.net cookies formsauthentication

2
推荐指数
1
解决办法
4720
查看次数

将ActionLink中的returnUrl从视图传递到重定向回视图的登录表单

我已经阅读了很多关于returnUrl的 SO帖子,因为它默认情况下与表单身份验证有关[authorize] MyController,但是我没有阅读任何关于简单地传递returnUrl的内容,其中唯一的身份验证发生在具有登录或注册表单和匿名用户的[HttpPost]之后.在这种情况下,我希望重定向来自原始链接并传递给通过表单身份验证对用户进行身份验证的操作.此重定向应该使用户返回到正在查看的页面1)单击注册或登录ActionLinks然后2)成功提交表单.这是在开发服务器上,因此HTTPS不是ATM的要求.

这是没有传递returnUrl的必要语法/代码的元素

_LoginPartial:

<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null}, htmlAttributes: new { id = "registerLink" })</li> //returnUrl???
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li> // returnUrl???
Run Code Online (Sandbox Code Playgroud)

登录视图:

@using (Html.BeginForm()){...} //new { returnUrl } ???
Run Code Online (Sandbox Code Playgroud)

登录获取ActionResult:

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{   
    //There is other ways to store the route
    TempData["ReturnUrl"] = returnUrl;
    return View();
}
Run Code Online (Sandbox Code Playgroud)

登录发布ActionResult:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model)
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password,     persistCookie: …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc returnurl razor formsauthentication

2
推荐指数
2
解决办法
2万
查看次数

Formsauthentication 和 owin facebook 身份验证并排(网络表单)

我需要使用表单身份验证将 Facebook(以及后来的 google 等)身份验证添加到现有的 asp.net webforms 应用程序。

我快到了,但表单身份验证和 owin 身份验证之间似乎存在冲突。我的解决方案基于标准的 VS 2015 模板。

重定向到外部身份验证提供程序(facebook)的相关代码如下:

string redirectUrl =
                ResolveUrl(String.Format(CultureInfo.InvariantCulture,
                    "~/Account/RegisterExternalLogin?{0}={1}&returnUrl={2}", IdentityHelper.ProviderNameKey,
                    provider, ReturnUrl));
            var properties = new AuthenticationProperties { RedirectUri = redirectUrl };

            Context.GetOwinContext().Authentication.Challenge(properties, provider);
            Response.StatusCode = 401;
            Response.End();
Run Code Online (Sandbox Code Playgroud)

如果我关闭表单身份验证,这会起作用。如果我有表单身份验证,用户将被重定向到 web.config 中定义的表单身份验证 URL:

    <authentication mode="Forms">
      <forms name=".ASPXAUTH_Test" loginUrl="~/start/login.aspx" timeout="60" >
        </forms>
    </authentication>
Run Code Online (Sandbox Code Playgroud)

根据我的理解,Http 状态代码 (401) 会触发重定向到 web.config 中的 Url。我试图设置其他状态代码,但它们根本不起作用。当我在 web.config 中关闭表单身份验证时,实际的登录过程仍然有效(令人惊讶)但是如果我在未登录的情况下访问受保护的页面,则会得到一个丑陋的 IIS 错误页面,而不是被重定向。

看来,我无法获得工作表单身份验证和外部身份验证才能正常工作:-(

到目前为止,所有替代方案对我来说似乎都不诱人: - 切换到身份框架(在我们的特定环境中,这绝对不是一个选项。我只是为了完整性才提到它) - 尝试使用 web.api 或其他东西类似(可能有同样的问题) - 放弃 owin 外部身份验证并手动实现所有内容

有没有人能够完成这项工作?任何帮助表示赞赏。提前致谢。

asp.net webforms formsauthentication owin

1
推荐指数
1
解决办法
632
查看次数

测试帐户/登录操作

我正在使用Visual Studio 2010的内置测试工具和本文中的类库来测试Account/Loggon操作,以创建虚假的控制器上下文.当我运行测试方法时,这段代码行:

  FormsAuthentication.SetAuthCookie(username, false);    
Run Code Online (Sandbox Code Playgroud)

抛出异常:对象引用未设置为对象的实例

为了测试loggon动作,我想我应该创建一个带有伪控制器上下文的控制器,它具有cookie集合.这是我的测试代码块:

   AccountController controller = new AccountController();
   var cookies = new HttpCookieCollection();

   controller.ControllerContext = new FakeControllerContext(controller, cookies);

   ActionResult result = controller.RemoteLogOn(username, password);
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc unit-testing controllercontext formsauthentication

0
推荐指数
1
解决办法
2518
查看次数