小编Mar*_*cus的帖子

在ASP.NET MVC中文件上载超过允许的大小时显示自定义错误页面

我的主要问题是,当上传的文件超出允许的大小时,我想显示自定义错误页面(web.config中的maxRequestLength).

上传大文件时,在调用控制器中的上传操作方法之前会抛出HttpException.这是预料之中的.

我试图在自定义属性中捕获异常,并在控制器中覆盖OnException.为什么不能在属性或OnException方法中捕获异常?

虽然可以在global.asax中捕获Application_Error中的异常,但Response.Redirect和Server.Transfer都不能用于重定向到自定义错误页面.Server.Transfer给出"未能处理子请求"错误,而response.redirect给出"已发送Http头"错误.

有任何想法吗?

提前致谢!

马库斯

c# asp.net asp.net-mvc iis-7 iis-6

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

使用FormsAuthenticationTicket创建非持久性cookie

我在使用FormsAuthenticationTicket创建非持久性cookie时遇到问题.我想在票证中存储userdata,因此我无法使用FormsAuthentication.SetAuthCookie()或FormsAuthentication.GetAuthCookie()方法.因此,我需要创建FormsAuthenticationTicket并将其存储在HttpCookie中.

我的代码看起来像这样:

DateTime expiration = DateTime.Now.AddDays(7);

// Create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2,
    user.Email,
    DateTime.Now,
    expiration,
    isPersistent,
    userData,
    FormsAuthentication.FormsCookiePath);

// Create cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
cookie.Path = FormsAuthentication.FormsCookiePath;
if (isPersistent)
    cookie.Expires = expiration;

// Add cookie to response
HttpContext.Current.Response.Cookies.Add(cookie);
Run Code Online (Sandbox Code Playgroud)

当变量isPersistent为true时,一切正常并且cookie被保留.但是当isPersistent为false时,cookie似乎仍然存在.我在浏览器窗口中登录,关闭它并再次打开浏览器,我仍然登录.如何将cookie设置为非持久性?

非持久性cookie是否与会话cookie相同?cookie信息是存储在服务器上的sessiondata中还是在每次请求/响应服务器时传输的cookie?

cookies forms-authentication httpcookie

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

OWIN OpenID提供程序 - GetExternalLoginInfo()返回null

我在使用ASP.NET身份的ASP.NET MVC5应用程序中遇到OWIN OpenId提供程序的问题,并且基于具有个人用户帐户身份验证的VS2013模板.用于Google和LinkedIn的OWIN OpenID提供程序用于登录身份验证.

问题是看起来很随机; 即使登录验证成功,GetExternalLoginInfo()也会在LoginConfirmation回调中返回null.

var authManager = HttpContext.Current.GetOwinContext().Authentication;
var login = authManager.GetExternalLoginInfo();
Run Code Online (Sandbox Code Playgroud)

使用的提供商是Google(Microsoft.Owin.Security.Google 2.1.0)和LinkedIn(来自Owin.Security.Providers 1.3),两家提供商都会导致同样的问题.

有时它失败一次然后再次工作,但有时它会继续失败,直到AppPool被回收.

目前,应用程序的两个实例托管在同一Windows Azure虚拟机上的IIS中.每个实例都有自己的AppPool但设置相同(不同的子域).有时登录停止在一个实例上工作,但仍然在另一个实例上工作.

这个问题也在本地重现(IIS Express - VS2013).

任何人都遇到类似的OWIN OpenID身份验证问题?

Startup.Auth.cs看起来像这样:

public void ConfigureAuth(IAppBuilder app)
{
    // Enable the application to use a cookie to store information for the signed in user
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
    });
    // Use a cookie to temporarily store information about a user logging in with a third       party login provider
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    app.UseGoogleAuthentication(); …
Run Code Online (Sandbox Code Playgroud)

c# openid owin asp.net-mvc-5 asp.net-identity

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

如何取消异步NSURLConnection

在NSURLConnection上调用实例方法取消通常根本不取消连接.

我正在NSOperation中执行异步NSURLConnection.如果取消操作,则通过在NSURLConnection上调用cancel然后将连接设置为nil来停止连接.

有没有办法立即取消连接?大多数情况下,它会继续在后台运行,直到请求完成,即使连接都被取消并设置为nil.取消连接后释放连接,取消分配NSOperation子类.即便如此,异步连接仍在继续运行!

还有其他方法可以取消它吗?或者它的运行线程可以取消吗?

这是我的NSOperation子类main方法的代码片段:

      // Create and start asynchronous connection
      self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
      [request release];

      while(!isFinished) {
          if(self.isCancelled) {
             [self.connection cancel];
             self.connection = nil; 
             [self uploadCanceledDelegate];
             return;
          }

          // Special sleep because callbacks are executed on same thread as this loop
          [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
      }
Run Code Online (Sandbox Code Playgroud)

注意:仅在启用WiFi时才会出现此问题.使用3G/Edge/GPRS,它可以正常工作

iphone nsurlconnection

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