开发者

Unhandled Exception that does not throw or get caught in visual studio

I created an asp.net mvc 3 application that has a CustomIdentity. The only place I used this code is in the global.ascx file here:

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = Fo开发者_如何转开发rmsAuthentication.Decrypt(authCookie.Value);
            var identity = new CustomIdentity(authTicket);
            BasicRoleProvider roleProvider = new BasicRoleProvider();
            string[] userRoles = roleProvider.GetRolesForUser(identity.Name);
            var principal = new GenericPrincipal(identity, userRoles);
            Context.User = principal;
        }
    }

This code gets executed without error. After authentication, I get this error in the browser:

Type 'MyProj.Web.ViewModel.CustomIdentity' in assembly 'MyProj.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

No exception in visual studio. Here is how my identity looks:

public class CustomIdentity : IIdentity
{
    private System.Web.Security.FormsAuthenticationTicket ticket;

    public CustomIdentity(System.Web.Security.FormsAuthenticationTicket ticket)
    {
        this.ticket = ticket;
    }

    public string AuthenticationType
    {
        get { return "Custom"; }
    }

    public bool IsAuthenticated
    {
        get { return true; }
    }

    public string FriendlyName
    {
        get { return ticket.UserData; }
    }
    public string Name
    {
        get { return ticket.Name; }
    }
}


Mark CustomIdentity as serializable.

[Serializable]
public class CustomIdentity : IIdentity
{...}


Add a [Serializable] attribute:

[Serializable]
public class CustomIdentity : IIdentity
{
    ...

The reason you don't get the exception in Visual Studio is that something else already handles it. You can actually ask Visual Studio to stop regardless - this is done via Debug / Exceptions. You could find SerializationException in there and tick the "thrown" box.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜