首页 > 解决方案 > 如果用户名未存储在数据库中,如何将用户重定向到错误页面?网

问题描述

我想做的是,如果用户还没有存储在数据库中,那么他们应该被重定向到另一个站点(现在说谷歌)。问题是,我不能在我的 catch {} 块中使用 response.redirect。如果用户不在角色中,则会出现一个错误页面,显示“对象引用未设置为对象的实例”。包括堆栈跟踪。这是我在授权类中的代码:

public override string[] GetRolesForUser(string username)
    {
        try
        {
            if (securityDB.Database.Connection.State == System.Data.ConnectionState.Closed)
            {
                securityDB.Database.Connection.Open();
            }

            USER user = securityDB.USERs
            .Where(o => o.USER_NAME == username)
            .FirstOrDefault();

            var roles = from ur in user.USER_ROLE
                        from r in securityDB.ROLEs
                        where ur.ROLE_ID == r.ID
                        select r.ROLE_NAME;
            if (roles != null)
                return roles.ToArray();
            else

                return new string[] { }; ;

        }

        catch (Exception ex)
        {
            //Reroute user to www.google.com
            Debug.WriteLine(ex.Message);
            throw;
        }

        //return new string[] { };

    }

标签: c#asp.netmodel-view-controllerauthorization

解决方案


推荐阅读